Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Tic-Tac-Toe/Debug/Tic-Tac-Toe.exe
Binary file not shown.
Binary file added Tic-Tac-Toe/Debug/Tic-Tac-Toe.ilk
Binary file not shown.
Binary file added Tic-Tac-Toe/Debug/Tic-Tac-Toe.pdb
Binary file not shown.
Binary file added Tic-Tac-Toe/Tic-Tac-Toe.sdf
Binary file not shown.
22 changes: 22 additions & 0 deletions Tic-Tac-Toe/Tic-Tac-Toe.sln
Original file line number Diff line number Diff line change
@@ -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
Binary file added Tic-Tac-Toe/Tic-Tac-Toe.v12.suo
Binary file not shown.
14 changes: 14 additions & 0 deletions Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.log
Original file line number Diff line number Diff line change
@@ -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
Binary file added Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.obj
Binary file not shown.
Binary file added Tic-Tac-Toe/Tic-Tac-Toe/Debug/Tic-Tac-Toe.pch
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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\|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added Tic-Tac-Toe/Tic-Tac-Toe/Debug/stdafx.obj
Binary file not shown.
Binary file added Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.idb
Binary file not shown.
Binary file added Tic-Tac-Toe/Tic-Tac-Toe/Debug/vc120.pdb
Binary file not shown.
40 changes: 40 additions & 0 deletions Tic-Tac-Toe/Tic-Tac-Toe/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -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.

/////////////////////////////////////////////////////////////////////////////
229 changes: 229 additions & 0 deletions Tic-Tac-Toe/Tic-Tac-Toe/Tic-Tac-Toe.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
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;
}
Loading