Created
January 1, 2012 04:25
-
-
Save dbanetto/1546253 to your computer and use it in GitHub Desktop.
Tic Tac Toe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| //defines | |
| #define tablehieght 3 | |
| #define tablewidth 3 | |
| #define player1 -32 | |
| #define player2 -64 | |
| #define player1Token "()" | |
| #define player2Token "><" | |
| //Protypes | |
| void printBoard(void); | |
| void setVars(void); | |
| bool isVaild(int i); | |
| void editBlock(int i , int player); | |
| int isGameOver(); | |
| bool checkWin(int player); | |
| //Global vars | |
| int table[tablehieght][tablewidth]; | |
| unsigned short curPlayer = 1; | |
| bool isPlaying = true; | |
| bool firstrun = true; | |
| bool numpadmode = false; | |
| int main() { | |
| if (firstrun) { | |
| //Game info/tut | |
| cout << "Info:\nPlayer 1 has the token of " << player1Token << | |
| ".\nPlayer 2 has the token " << player2Token << | |
| ".\nThe game will end if there is no loser." << endl; | |
| //Getting settings | |
| cout << "Enable num-pad friendly mode?[y/n]" << endl; | |
| string str; | |
| cin >> str; | |
| if (str == "y" || str == "yes") | |
| numpadmode = true; | |
| firstrun = false; | |
| system("pause"); | |
| } | |
| setVars(); | |
| try { | |
| do { | |
| system("cls"); | |
| cout << "Player " << curPlayer << "'s turn["<< (curPlayer == 1 ? player1Token : player2Token) <<"]. Pick your spot[Number only]:" << endl; | |
| printBoard(); | |
| int input = NULL; | |
| cin >> input; | |
| if (input == NULL) { | |
| cout << "Omfg your input is not a number, you shouldn't do that.\nTic-Tac-Toe has stopped BECAUSE YOU BROKE IT."; | |
| return(1); | |
| } | |
| //Is quiting? | |
| if (input == -1) { | |
| break; | |
| } | |
| if (isVaild(input)) { | |
| editBlock(input , curPlayer); | |
| switch (curPlayer) { //Switch players turns | |
| case 1: | |
| curPlayer = 2; | |
| break; | |
| case 2: | |
| curPlayer = 1; | |
| break; | |
| } | |
| } else { | |
| cout << "Invalid move." << endl; | |
| } | |
| switch (isGameOver()) { | |
| case player1: | |
| system("cls"); | |
| cout << "Player 1 wins" << endl; | |
| printBoard(); | |
| isPlaying = false; | |
| break; | |
| case player2: | |
| system("cls"); | |
| cout << "Player 2 wins" << endl; | |
| printBoard(); | |
| isPlaying = false; | |
| break; | |
| case -1: | |
| system("cls"); | |
| cout << "Draw.\nEVERYONE LOSES." << endl; | |
| printBoard(); | |
| isPlaying = false; | |
| break; | |
| default: | |
| break; | |
| } | |
| } while(isPlaying); | |
| } catch(char *str) { | |
| cout << "Something went horribly wrong.\nTic-Tac-Toe is stopping." << endl << *str; | |
| return(1); | |
| } | |
| cout << "Play again?[y/n]"; | |
| string tmp; | |
| cin >> tmp; | |
| if (tmp == "y" || tmp == "yes") { | |
| system("cls"); | |
| main(); | |
| } | |
| return 0; | |
| } | |
| void printBoard() { | |
| for (int i = 0; i < tablehieght; i++) { | |
| for (int u = 0; u < tablewidth; u++) { | |
| //graphics | |
| if (u > 0) | |
| cout << '|'; | |
| if (table[i][u] == player1) { //nought | |
| cout << player1Token; | |
| continue; | |
| } else if (table[i][u] == player2) { //Cross | |
| cout << player2Token; | |
| continue; | |
| } | |
| if (table[i][u] < 10) //Keeping fixed width of charaters | |
| cout << 0 << table[i][u]; | |
| else | |
| cout << table[i][u]; | |
| } | |
| if (i + 1 > 0 && i + 1 != tablehieght) { | |
| cout << endl; | |
| for (int u = 0; u < tablewidth - 1; u++) { | |
| cout << "--+"; | |
| } | |
| cout << "--"; | |
| } | |
| cout << endl; | |
| } | |
| } | |
| void setVars() { | |
| isPlaying = true; | |
| if (!numpadmode) { | |
| int n = 1; | |
| for (int i = 0; i < tablehieght; i++) { | |
| for (int u = 0; u < tablewidth; u++) { | |
| table[i][u] = n++; | |
| } | |
| } | |
| } else { | |
| //Num pad friendly | |
| int n = tablehieght; | |
| for (int i = 0; i < tablehieght; i++) { | |
| for (int u = 0; u < tablewidth; u++) { | |
| table[i][u] = (n * tablewidth - 1) + (u -1); | |
| } | |
| n--; | |
| } | |
| } | |
| } | |
| bool isVaild(int i) { | |
| if (i < 1) | |
| return false; | |
| for (int o = 0; o < tablehieght; o++) { | |
| for (int u = 0; u < tablewidth; u++) { | |
| if (table[o][u] == i) | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| void editBlock(int i, int player) { | |
| short code = 0x0; | |
| switch (player) { | |
| case 1: | |
| code = player1; | |
| break; | |
| case 2: | |
| code = player2; | |
| break; | |
| } | |
| for (int o = 0; o < tablehieght; o++) { | |
| for (int u = 0; u < tablewidth; u++) { | |
| if (table[o][u] == i) | |
| table[o][u] = code; | |
| } | |
| } | |
| } | |
| int isGameOver() { //Output: -1 - Draw, no more blocks to choose from. 0 - Still going. Player# - That player won. | |
| bool isFull = true; | |
| for (int o = 0; o < tablehieght; o++) { | |
| for (int u = 0; u < tablewidth; u++) { | |
| if (table[o][u] > 0) { //If table[][] is filled with player1 or player2 isFull will remain true | |
| isFull = false; | |
| break; | |
| } | |
| } | |
| if (!isFull) | |
| break; | |
| } | |
| if(checkWin(player1) == player1) | |
| return player1; | |
| if (checkWin(player2) == player2) | |
| return player2; | |
| if (isFull) | |
| return -1; | |
| //Zero is it's still going | |
| return 0; | |
| } | |
| bool checkWin(int player) { | |
| for (int o = 0; o < tablehieght; o++) { | |
| bool win = true; | |
| for (int u = 0; u < tablewidth; u++) { | |
| if (table[o][u] != player) { //Checking downwards. If there is anything in the column that's not player, player cannot of won | |
| win = false; | |
| } | |
| } | |
| if (win) | |
| return win; | |
| } | |
| for (int o = 0; o < tablewidth; o++) { | |
| bool win = true; | |
| for (int u = 0; u < tablehieght; u++) { | |
| if (table[u][o] != player) { //Checking rows. If there is anything in the row that's not player, player cannot of won | |
| win = false; | |
| } | |
| } | |
| if (win) | |
| return win; | |
| } | |
| int i = 0; //Diagonal (0,0 to tablehieght,tablewidth) | |
| bool win = true; | |
| for (int o = 0; o < tablehieght; o++) { | |
| if (table[o][i++] != player) { | |
| win = false; | |
| } | |
| } | |
| if (win) | |
| return win; | |
| i = tablehieght; | |
| win = true; //Diagonal (tablehieght,0 to 0,tablewidth) | |
| for (int o = 0; o < tablehieght; o++) { | |
| if (table[o][i--] != player) { | |
| win = false; | |
| } | |
| } | |
| if (win) | |
| return win; | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment