diff --git a/Tic-Tac-Toe/Debug/Tic-Tac-Toe.exe b/Tic-Tac-Toe/Debug/Tic-Tac-Toe.exe new file mode 100644 index 0000000..3a2f8cf Binary files /dev/null and b/Tic-Tac-Toe/Debug/Tic-Tac-Toe.exe differ diff --git a/Tic-Tac-Toe/Debug/Tic-Tac-Toe.ilk b/Tic-Tac-Toe/Debug/Tic-Tac-Toe.ilk new file mode 100644 index 0000000..47cb5cb Binary files /dev/null and b/Tic-Tac-Toe/Debug/Tic-Tac-Toe.ilk differ diff --git a/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pdb b/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pdb new file mode 100644 index 0000000..504cbe0 Binary files /dev/null and b/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pdb differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe.sdf b/Tic-Tac-Toe/Tic-Tac-Toe.sdf new file mode 100644 index 0000000..d597ae3 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe.sdf differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe.sln b/Tic-Tac-Toe/Tic-Tac-Toe.sln new file mode 100644 index 0000000..dcbb6c8 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.30501.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tic-Tac-Toe", "Tic-Tac-Toe\Tic-Tac-Toe.vcxproj", "{11767904-DF35-4F86-9EAE-FB6A619E36E6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {11767904-DF35-4F86-9EAE-FB6A619E36E6}.Debug|Win32.ActiveCfg = Debug|Win32 + {11767904-DF35-4F86-9EAE-FB6A619E36E6}.Debug|Win32.Build.0 = Debug|Win32 + {11767904-DF35-4F86-9EAE-FB6A619E36E6}.Release|Win32.ActiveCfg = Release|Win32 + {11767904-DF35-4F86-9EAE-FB6A619E36E6}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Tic-Tac-Toe/Tic-Tac-Toe.v12.suo b/Tic-Tac-Toe/Tic-Tac-Toe.v12.suo new file mode 100644 index 0000000..b88f23b Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe.v12.suo differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.log b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.log new file mode 100644 index 0000000..86dc6e6 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.log @@ -0,0 +1,14 @@ +Build started 1/30/2015 5:51:25 AM. +Project "C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\Tic-Tac-Toe\Tic-Tac-Toe.vcxproj" on node 2 (Build target(s)). +ClCompile: + C:\Program Files\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Yu"stdafx.h" /Fp"Debug\Tic-Tac-Toe.pch" /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt "Tic-Tac-Toe.cpp" + Tic-Tac-Toe.cpp +Link: + C:\Program Files\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\Debug\Tic-Tac-Toe.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\Debug\Tic-Tac-Toe.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\Debug\Tic-Tac-Toe.lib" /MACHINE:X86 Debug\stdafx.obj + "Debug\Tic-Tac-Toe.obj" + Tic-Tac-Toe.vcxproj -> C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\Debug\Tic-Tac-Toe.exe +Done Building Project "C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\Tic-Tac-Toe\Tic-Tac-Toe.vcxproj" (Build target(s)). + +Build succeeded. + +Time Elapsed 00:00:02.36 diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.obj b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.obj new file mode 100644 index 0000000..ced9407 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.obj differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pch b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pch new file mode 100644 index 0000000..d48d183 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pch differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/CL.read.1.tlog b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/CL.read.1.tlog new file mode 100644 index 0000000..7112fcf Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/CL.read.1.tlog differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/CL.write.1.tlog b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/CL.write.1.tlog new file mode 100644 index 0000000..a3593b2 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/CL.write.1.tlog differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/Tic-Tac-Toe.lastbuildstate b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/Tic-Tac-Toe.lastbuildstate new file mode 100644 index 0000000..c2b1fbc --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/Tic-Tac-Toe.lastbuildstate @@ -0,0 +1,2 @@ +#TargetFrameworkVersion=v4.0:PlatformToolSet=v120:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit +Debug|Win32|C:\Users\IEUser\Dropbox\tech\Cpp_Lab_03\Tic-Tac-Toe\| diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/cl.command.1.tlog b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/cl.command.1.tlog new file mode 100644 index 0000000..79717c2 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/cl.command.1.tlog differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.command.1.tlog b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.command.1.tlog new file mode 100644 index 0000000..4975231 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.command.1.tlog differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.read.1.tlog b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.read.1.tlog new file mode 100644 index 0000000..af624ba Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.read.1.tlog differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.write.1.tlog b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.write.1.tlog new file mode 100644 index 0000000..19291df Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.tlog/link.write.1.tlog differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/stdafx.obj b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/stdafx.obj new file mode 100644 index 0000000..376daff Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/stdafx.obj differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.idb b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.idb new file mode 100644 index 0000000..65ec07d Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.idb differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.pdb b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.pdb new file mode 100644 index 0000000..a4aaa23 Binary files /dev/null and b/Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.pdb differ diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/ReadMe.txt b/Tic-Tac-Toe/Tic-Tac-Toe/ReadMe.txt new file mode 100644 index 0000000..4e3557f --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/ReadMe.txt @@ -0,0 +1,40 @@ +======================================================================== + CONSOLE APPLICATION : Tic-Tac-Toe Project Overview +======================================================================== + +AppWizard has created this Tic-Tac-Toe application for you. + +This file contains a summary of what you will find in each of the files that +make up your Tic-Tac-Toe application. + + +Tic-Tac-Toe.vcxproj + This is the main project file for VC++ projects generated using an Application Wizard. + It contains information about the version of Visual C++ that generated the file, and + information about the platforms, configurations, and project features selected with the + Application Wizard. + +Tic-Tac-Toe.vcxproj.filters + This is the filters file for VC++ projects generated using an Application Wizard. + It contains information about the association between the files in your project + and the filters. This association is used in the IDE to show grouping of files with + similar extensions under a specific node (for e.g. ".cpp" files are associated with the + "Source Files" filter). + +Tic-Tac-Toe.cpp + This is the main application source file. + +///////////////////////////////////////////////////////////////////////////// +Other standard files: + +StdAfx.h, StdAfx.cpp + These files are used to build a precompiled header (PCH) file + named Tic-Tac-Toe.pch and a precompiled types file named StdAfx.obj. + +///////////////////////////////////////////////////////////////////////////// +Other notes: + +AppWizard uses "TODO:" comments to indicate parts of the source code you +should add to or customize. + +///////////////////////////////////////////////////////////////////////////// diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.cpp b/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.cpp new file mode 100644 index 0000000..49f0557 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.cpp @@ -0,0 +1,229 @@ +// Tic-Tac-Toe.cpp : Defines the entry point for the console application. +// + +// Tic-Tac-Toe.cpp : Defines the entry point for the console application. +// + +#include "stdafx.h" +#include +using namespace std; + +/******************* +* Brandon McCurry +* play Tic-Tac-Toe +*******************/ + +void display_board(char[]); +char winner(char[]); +int human_move(char[]); +int computer_move(char[], char, char); + +int main() +{ + //try strings if classes needed + char turn = 'X'; + //display_instruct + cout << "Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe. \n" + << "This will be a showdown between your human brain and my silicon processor.\n" + << "You will make your move known by entering a number, 0 - 8. The number \n" + << "will correspond to the board position as illustrated : \n" + << endl << endl + << "0 | 1 | 2\n" + << "-------- -\n" + << "3 | 4 | 5\n" + << "-------- -\n" + << "6 | 7 | 8\n" + << "Prepare yourself, human. The ultimate battle is about to begin. \n"; + //end of display_instruct + //pieces + char go_first = 'a'; + char human = 'X'; + char computer = 'O'; + //ask_yes_no + cout << "Do you require the first move? (y/n): "; + cin >> go_first; + cin.sync(); + while (tolower(go_first) != 'y' && tolower(go_first) != 'n') + { + cout << "Do you require the first move? (y/n): "; + cin >> go_first; + cin.sync(); + } + //end of ask_yes_no + if (go_first == 'n') + { + cout << "\nYour bravery will be your undoing... I will go first." << endl; + human = 'O'; + computer = 'X'; + } + else + { + cout << "\nThen take the first move. You will need it." << endl; + } + //end of pieces + //new_board + char board[9]; + for (int i = 0; i < 9; i++) + { + board[i] = ' '; + } + //end of new board + display_board(board); + int move = 0; + char result = winner(board); + while (result != 'X' && result != 'O' && result != 'T') + { + if (turn == human) + { + move = human_move(board); + board[move] = human; + } + else + { + move = computer_move(board, computer, human); + board[move] = computer; + cout << move; + } + display_board(board); + if (turn == 'X') + { + turn = 'O'; + } + else + { + turn = 'X'; + } + result = winner(board); + } + if (result != 'T') + { + cout << result << " won!" << endl; + if (result == computer) + { + cout << "As I predicted, human, I am triumphant once more. \n" + << "Proof that computers are superior to humans in all regards." << endl; + } + else if (result == human) + { + cout << "No, no! It cannot be! Somehow you tricked me, human. \n" + << "But never again! I, the computer, so swear it!" << endl; + } + } + else + { + cout << "It's a tie!" << endl; + cout << "You were most lucky, human, and somehow managed to tie me. \n" + << "Celebrate today... for this is the best you will ever achieve."; + } +} +int computer_move(char board[], char computer, char human) +{ + int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 }; + cout << "I shall take square number "; + char result = 'a'; + int retVal = 8; + int i = 0; + //if cannot win or block, place at next best + while (i < 9) + { + if (board[BEST_MOVES[i]] == ' ') + { + retVal = BEST_MOVES[i]; + i = 9; + } + i++; + } + //if human can win, block that move + for (int i = 0; i < 9; i++) + { + if (board[i] == ' ') + { + board[i] = human; + result = winner(board); + if (result == human) + { + retVal = i; + } + board[i] = ' '; + } + } + //if computer can win, take that move + for (int i = 0; i < 9; i++) + { + if (board[i] == ' ') + { + board[i] = computer; + result = winner(board); + if (result == computer) + { + retVal = i; + } + board[i] = ' '; + } + } + return retVal; +} + +int human_move(char board[]) +{ + int response = 0; + cout << "Where will you move? (0 - 8): "; + cin >> response; + while (response < 0 || response > 8) + { + if (board[response] != ' ') + { + cout << "That square is already occupied, foolish human. " + << "Choose another. " << endl; + } + cout << "Where will you move? (0 - 8): "; + cin >> response; + } + cout << "Fine..." << endl; + return response; +} +char winner(char board[]) +{ + char winner = 'a'; + int WAYS_TO_WIN[8][3] = { { 0, 1, 2 }, + { 3, 4, 5 }, + { 6, 7, 8 }, + { 0, 3, 6 }, + { 1, 4, 7 }, + { 2, 5, 8 }, + { 0, 4, 8 }, + { 2, 4, 6 } }; + bool tie = true; + for (int i = 0; i < 8; i++) + { + if (board[WAYS_TO_WIN[i][0]] == board[WAYS_TO_WIN[i][1]] && + board[WAYS_TO_WIN[i][1]] == board[WAYS_TO_WIN[i][2]]) + { + if (board[WAYS_TO_WIN[i][1]] != ' ') + { + winner = board[WAYS_TO_WIN[i][0]]; + return winner; + } + } + if (board[i] == ' ') + { + tie = false; + } + } + if (tie == true) + { + return 'T'; + } + else + { + return 'F'; + } +} + +void display_board(char board[]) +{ + cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2] + << "\n\t" << "---------" << "\n\t" << board[3] << " | " << board[4] + << " | " << board[5] << "\n\t" << "---------" << "\n\t" << board[6] + << " | " << board[7] << " | " << board[8] << endl; +} \ No newline at end of file diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.vcxproj b/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.vcxproj new file mode 100644 index 0000000..e965d56 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.vcxproj @@ -0,0 +1,95 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {11767904-DF35-4F86-9EAE-FB6A619E36E6} + Win32Proj + TicTacToe + + + + Application + true + v120 + Unicode + + + Application + false + v120 + true + Unicode + + + + + + + + + + + + + true + + + false + + + + Use + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + Use + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;_LIB;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + Create + Create + + + + + + + \ No newline at end of file diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.vcxproj.filters b/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.vcxproj.filters new file mode 100644 index 0000000..6ac3433 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/stdafx.cpp b/Tic-Tac-Toe/Tic-Tac-Toe/stdafx.cpp new file mode 100644 index 0000000..338744d --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// Tic-Tac-Toe.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/stdafx.h b/Tic-Tac-Toe/Tic-Tac-Toe/stdafx.h new file mode 100644 index 0000000..b005a83 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here diff --git a/Tic-Tac-Toe/Tic-Tac-Toe/targetver.h b/Tic-Tac-Toe/Tic-Tac-Toe/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/Tic-Tac-Toe/Tic-Tac-Toe/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include diff --git a/Tic-Tac-Toe/ipch/tic-tac-toe-b38de6b9/tic-tac-toe-3c851666.ipch b/Tic-Tac-Toe/ipch/tic-tac-toe-b38de6b9/tic-tac-toe-3c851666.ipch new file mode 100644 index 0000000..0114f62 Binary files /dev/null and b/Tic-Tac-Toe/ipch/tic-tac-toe-b38de6b9/tic-tac-toe-3c851666.ipch differ diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..45e99be --- /dev/null +++ b/test.txt @@ -0,0 +1,226 @@ +// Tic-Tac-Toe.cpp : Defines the entry point for the console application. +// + +#include "stdafx.h" +#include +using namespace std; + +/******************* +* Brandon McCurry +* play Tic-Tac-Toe +*******************/ + +void display_board(char[]); +char winner(char[]); +int human_move(char[]); +int computer_move(char[], char, char); + +int main() +{ + //try strings if classes needed + char turn = 'X'; + //display_instruct + cout << "Welcome to the greatest intellectual challenge of all time: Tic-Tac-Toe. \n" + << "This will be a showdown between your human brain and my silicon processor.\n" + << "You will make your move known by entering a number, 0 - 8. The number \n" + << "will correspond to the board position as illustrated : \n" + << endl << endl + << "0 | 1 | 2\n" + << "-------- -\n" + << "3 | 4 | 5\n" + << "-------- -\n" + << "6 | 7 | 8\n" + << "Prepare yourself, human. The ultimate battle is about to begin. \n"; + //end of display_instruct + //pieces + char go_first = 'a'; + char human = 'X'; + char computer = 'O'; + //ask_yes_no + cout << "Do you require the first move? (y/n): "; + cin >> go_first; + cin.sync(); + while (tolower(go_first) != 'y' && tolower(go_first) != 'n') + { + cout << "Do you require the first move? (y/n): "; + cin >> go_first; + cin.sync(); + } + //end of ask_yes_no + if (go_first == 'n') + { + cout << "\nYour bravery will be your undoing... I will go first." << endl; + human = 'O'; + computer = 'X'; + } + else + { + cout << "\nThen take the first move. You will need it." << endl; + } + //end of pieces + //new_board + char board[9]; + for (int i = 0; i < 9; i++) + { + board[i] = ' '; + } + //end of new board + display_board(board); + int move = 0; + char result = winner(board); + while (result != 'X' && result != 'O' && result != 'T') + { + if (turn == human) + { + move = human_move(board); + board[move] = human; + } + else + { + move = computer_move(board, computer, human); + board[move] = computer; + cout << move; + } + display_board(board); + if (turn == 'X') + { + turn = 'O'; + } + else + { + turn = 'X'; + } + result = winner(board); + } + if (result != 'T') + { + cout << result << " won!" << endl; + if (result == computer) + { + cout << "As I predicted, human, I am triumphant once more. \n" + << "Proof that computers are superior to humans in all regards." << endl; + } + else if (result == human) + { + cout << "No, no! It cannot be! Somehow you tricked me, human. \n" + << "But never again! I, the computer, so swear it!" << endl; + } + } + else + { + cout << "It's a tie!" << endl; + cout << "You were most lucky, human, and somehow managed to tie me. \n" + << "Celebrate today... for this is the best you will ever achieve."; + } +} +int computer_move(char board[], char computer, char human) +{ + int BEST_MOVES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7}; + cout << "I shall take square number "; + char result = 'a'; + int retVal = 8; + int i = 0; + //if cannot win or block, place at next best + while (i < 9) + { + if (board[BEST_MOVES[i]] == ' ') + { + retVal = BEST_MOVES[i]; + i = 9; + } + i++; + } + //if human can win, block that move + for (int i = 0; i < 9; i ++) + { + if (board[i] == ' ') + { + board[i] = human; + result = winner(board); + if (result == human) + { + retVal = i; + } + board[i] = ' '; + } + } + //if computer can win, take that move + for (int i = 0; i < 9; i++) + { + if (board[i] == ' ') + { + board[i] = computer; + result = winner(board); + if (result == computer) + { + retVal = i; + } + board[i] = ' '; + } + } + return retVal; +} + +int human_move(char board[]) +{ + int response = 0; + cout << "Where will you move? (0 - 8): "; + cin >> response; + while (response < 0 || response > 8) + { + if (board[response] != ' ') + { + cout << "That square is already occupied, foolish human. " + << "Choose another. " << endl; + } + cout << "Where will you move? (0 - 8): "; + cin >> response; + } + cout << "Fine..." << endl; + return response; +} +char winner(char board[]) +{ + char winner = 'a'; + int WAYS_TO_WIN[8][3] = { { 0, 1, 2 }, + { 3, 4, 5 }, + { 6, 7, 8 }, + { 0, 3, 6 }, + { 1, 4, 7 }, + { 2, 5, 8 }, + { 0, 4, 8 }, + { 2, 4, 6 } }; + bool tie = true; + for (int i = 0; i < 8; i++) + { + if (board[WAYS_TO_WIN[i][0]] == board[WAYS_TO_WIN[i][1]] && + board[WAYS_TO_WIN[i][1]] == board[WAYS_TO_WIN[i][2]]) + { + if(board[WAYS_TO_WIN[i][1]] != ' ') + { + winner = board[WAYS_TO_WIN[i][0]]; + return winner; + } + } + if (board[i] == ' ') + { + tie = false; + } + } + if(tie == true) + { + return 'T'; + } + else + { + return 'F'; + } +} + +void display_board(char board[]) +{ + cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2] + << "\n\t" << "---------" << "\n\t" << board[3] << " | " << board[4] + << " | " << board[5] << "\n\t" << "---------" << "\n\t" << board[6] + << " | " << board[7] << " | " << board[8] << endl; +} \ No newline at end of file