1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

// 6_TicTacToe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct player {
int move;
} p1[9], p2[9];
bool checkMarks(int move, player p[], int index){ // Check if move exists in history
for (int i=0; i < index; i++){
if (move == p[i].move)
return true;
}
return false;
}
bool chickenDinner(char squares[], bool& winner, char mark) { // Winner, Winner, Chicken Dinner  Check
if (squares[0] == mark && squares[1] == mark && squares[2] == mark) {
return true;
}else if (squares[3] == mark && squares[4] == mark && squares[5] == mark){
return true;
}else if (squares[6] == mark && squares[7] == mark && squares[8] == mark){
return true;
}else if (squares[0] == mark && squares[4] == mark && squares[8] == mark){
return true;
}else if (squares[2] == mark && squares[4] == mark && squares[6] == mark){
return true;
}else if (squares[0] == mark && squares[3] == mark && squares[6] == mark){
return true;
}else if (squares[1] == mark && squares[4] == mark && squares[7] == mark){
return true;
}else if (squares[2] == mark && squares[5] == mark && squares[8] == mark){
return true;
}else{
return false;
}
}
void drawBoard(char squares[]) { // Board
system("cls");
cout << "\n\n\tTic Tac Toe\n\n";
cout << "Player 1 (X)\tPlayer 2(O)\n\n";
cout << "   " << endl;
cout << " " << squares[0] << "  " << squares[1] << "  " << squares[2] << endl;
cout << "_______________" << endl;
cout << "   " << endl;
cout << " " << squares[3] << "  " << squares[4] << "  " << squares[5] << endl;
cout << "_______________" << endl;
cout << "   " << endl;
cout << " " << squares[6] << "  " << squares[7] << "  " << squares[8] << endl << endl << endl;
}
void play(char squares[]) { // Get player moves and load other functions
drawBoard(squares);
int count = 1;
bool winner = false;
int indexP1 = 0;
int indexP2 = 0;
char mark = 'Z';
int player = 0;
while (winner == false) {
for (int i=0; i<9; i++) {
bool used = true;
bool cont = true;
if (count % 2 != 0) {
mark = 'X';
while (used == true  p1[indexP1].move > 8) {
cout << "\aPlayer 1 (X) please select a block to mark.";
cin >> p1[indexP1].move;
used = checkMarks(p1[indexP1].move, p2, indexP1);
}
squares[p1[indexP1].move] = 'X';
indexP1++;
count++;
}else{
mark = 'O';
while (used == true p1[indexP1].move > 8) {
cout << "\aPlayer 2 (O) please select a block to mark.";
cin >> p2[indexP2].move;
used = checkMarks(p2[indexP2].move, p1, indexP2);
}
squares[p2[indexP2].move] = 'O';
indexP2++;
count;
}
while (cont == true) {
drawBoard(squares);
winner = chickenDinner(squares, winner, mark);
cont = false;
}
if (winner == true && mark == 'X'){
player = 1;
break;
}else if (winner == true && mark == 'O'){
player = 2;
break;
}
}
}
cout << "\n\n\a\a\a\aPlayer " << player << " wins!\n\n" << endl;
}
int main()
{
char squares[9] = { '0', '1', '2', '3', '4', '5', '6', '7', '8' };
play(squares);
cin.get();
}
