|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +//initialising array |
| 5 | +char square[10] = {'o','1','2','3','4','5','6','7','8','9'}; |
| 6 | + |
| 7 | +int checkwin(); |
| 8 | +void board(); |
| 9 | + |
| 10 | +int main() |
| 11 | +{ |
| 12 | + int player = 1,i,choice; |
| 13 | + |
| 14 | + char mark; |
| 15 | + do |
| 16 | + { |
| 17 | + board(); |
| 18 | + player=(player%2)?1:2; |
| 19 | + |
| 20 | + cout << "Player " << player << ", enter a number: "; |
| 21 | + cin >> choice; |
| 22 | + |
| 23 | + mark=(player == 1) ? 'X' : 'O'; |
| 24 | + |
| 25 | + if (choice == 1 && square[1] == '1') |
| 26 | + |
| 27 | + square[1] = mark; |
| 28 | + else if (choice == 2 && square[2] == '2') |
| 29 | + |
| 30 | + square[2] = mark; |
| 31 | + else if (choice == 3 && square[3] == '3') |
| 32 | + |
| 33 | + square[3] = mark; |
| 34 | + else if (choice == 4 && square[4] == '4') |
| 35 | + |
| 36 | + square[4] = mark; |
| 37 | + else if (choice == 5 && square[5] == '5') |
| 38 | + |
| 39 | + square[5] = mark; |
| 40 | + else if (choice == 6 && square[6] == '6') |
| 41 | + |
| 42 | + square[6] = mark; |
| 43 | + else if (choice == 7 && square[7] == '7') |
| 44 | + |
| 45 | + square[7] = mark; |
| 46 | + else if (choice == 8 && square[8] == '8') |
| 47 | + |
| 48 | + square[8] = mark; |
| 49 | + else if (choice == 9 && square[9] == '9') |
| 50 | + |
| 51 | + square[9] = mark; |
| 52 | + else |
| 53 | + { |
| 54 | + cout<<"Invalid move "; |
| 55 | + |
| 56 | + player--; |
| 57 | + cin.ignore(); |
| 58 | + cin.get(); |
| 59 | + } |
| 60 | + i=checkwin(); |
| 61 | + |
| 62 | + player++; |
| 63 | + }while(i==-1); |
| 64 | + board(); |
| 65 | + if(i==1) |
| 66 | + |
| 67 | + cout<<"==>\aPlayer "<<--player<<" win "; |
| 68 | + else |
| 69 | + cout<<"==>\aGame draw"; |
| 70 | + |
| 71 | + cin.ignore(); |
| 72 | + cin.get(); |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +/* |
| 77 | + FUNCTION TO RETURN GAME STATUS |
| 78 | + 1 FOR GAME IS OVER WITH RESULT |
| 79 | + -1 FOR GAME IS IN PROGRESS |
| 80 | + O GAME IS OVER AND NO RESULT |
| 81 | +*/ |
| 82 | + |
| 83 | +int checkwin() |
| 84 | +{ |
| 85 | + if (square[1] == square[2] && square[2] == square[3]) |
| 86 | + |
| 87 | + return 1; |
| 88 | + else if (square[4] == square[5] && square[5] == square[6]) |
| 89 | + |
| 90 | + return 1; |
| 91 | + else if (square[7] == square[8] && square[8] == square[9]) |
| 92 | + |
| 93 | + return 1; |
| 94 | + else if (square[1] == square[4] && square[4] == square[7]) |
| 95 | + |
| 96 | + return 1; |
| 97 | + else if (square[2] == square[5] && square[5] == square[8]) |
| 98 | + |
| 99 | + return 1; |
| 100 | + else if (square[3] == square[6] && square[6] == square[9]) |
| 101 | + |
| 102 | + return 1; |
| 103 | + else if (square[1] == square[5] && square[5] == square[9]) |
| 104 | + |
| 105 | + return 1; |
| 106 | + else if (square[3] == square[5] && square[5] == square[7]) |
| 107 | + |
| 108 | + return 1; |
| 109 | + else if (square[1] != '1' && square[2] != '2' && square[3] != '3' |
| 110 | + && square[4] != '4' && square[5] != '5' && square[6] != '6' |
| 111 | + && square[7] != '7' && square[8] != '8' && square[9] != '9') |
| 112 | + |
| 113 | + return 0; |
| 114 | + else |
| 115 | + return -1; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +/* |
| 120 | + FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK |
| 121 | +*/ |
| 122 | + |
| 123 | + |
| 124 | +void board() |
| 125 | +{ |
| 126 | + system("cls"); |
| 127 | + cout << "\n\n\tTic Tac Toe\n\n"; |
| 128 | + |
| 129 | + cout << "Player 1 (X) - Player 2 (O)" << endl << endl; |
| 130 | + cout << endl; |
| 131 | + |
| 132 | + cout << " | | " << endl; |
| 133 | + cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl; |
| 134 | + |
| 135 | + cout << "_____|_____|_____" << endl; |
| 136 | + cout << " | | " << endl; |
| 137 | + |
| 138 | + cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl; |
| 139 | + |
| 140 | + cout << "_____|_____|_____" << endl; |
| 141 | + cout << " | | " << endl; |
| 142 | + |
| 143 | + cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl; |
| 144 | + |
| 145 | + cout << " | | " << endl << endl; |
| 146 | +} |
0 commit comments