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
|
#include<iostream>
using namespace std;
void checkwin(char t[], char turn);
void board(char t[]);
void play(char t[], char turn);
int main(){
char t[]={'1','2','3','4','5','6','7','8','9'}, turn='x';
int x = 0;
int o = 0;
cout << "Welcome to Tic Tac Toe! Game is over after 9 moves. Press enter to start\n";
cin.ignore();///makes you press enter
cout << "Player one goes first! You are X! Choose a space\n";
board(t);
while(o<9) {
play(t, turn);
board(t);
checkwin(t, turn);
if(turn=='x'){
cout << "Player 2: ";
turn='o';
}
else{
cout << "Player 1:";
turn='x';
}
o++;
}
}
//FUNCTIONS
void board(char t[]){
///prints and updates the board
cout << t[0] << " | " << t[1] << " | " << t[2] << "\n";
cout << "----------" << endl;
cout << t[3] << " | " << t[4] << " | " << t[5] << "\n";
cout << "----------" << endl;
cout << t[6] << " | " << t[7] << " | " << t[8] << "\n";
}
void play(char t[], char turn){
//decides where to put the "x" or "o"
int pos;
cin >> pos;
if(t[pos-1] =='x' || t[pos-1]=='o'){/// I have to subtract one because t[0] = 1 and t[8] = 9
cout << "\nThat space was taken! You lose your turn!\n";
return;
}
t[pos-1]=turn;
}
void checkwin(char t[], char turn) {
if(t[0] == 'x' && t[1] == 'x' && t[2] =='x'){
cout << "\n X wins! GG! \n";
return;
}
}
|