i'm creating a tic tac toe game (no functions unfortunately.....sorry!) and was trying to check for a winner at the end of each turn. Is there any way of doing this without using a hefty amount of "if" statements? I have already searched the forum but the only examples I could find included these statements in mass.
I was thinking to using a nested "for" loop but the problem came that I would only be searching row wins and column wins...i'm stumped. If you would like to see my code, see below. Like I said, there is no check yet, but I added a comment line so you can see where it would start.
Once again, I'm very sorry about the lack of functions, I am trying to go step by step in my book and every chapter I try to make a game out of what I've learned so far (and very limited, yes).Code://Tic Tac Toe //Dustin Hardin 12-12-08 //Play the classic game of tic tac toe #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; int main() { const short NUM_ROWS = 3, NUM_COLUMNS = 3; const short unsigned MAX_PLAYERS = 2; short unsigned numPlayers, numComputer, rowSelect, columnSelect, xCheck = 0, oCheck = 0; bool playerMove, gameBoardFull = true; short unsigned int whoFirst, whoWin = 0, move, playerCheck, opponentCheck; srand(time(0)); string players[MAX_PLAYERS]; string board[NUM_ROWS][NUM_COLUMNS] = { {"|#|","|#|","|#|"}, {"|#|","|#|","|#|"}, {"|#|","|#|","|#|"} }; while (true) //Game Loop { //get number of players do{ system("cls"); cout << "\t\tWelcome to Tic-Tac-Toe!\n\n"; cout << "How many players will be joining us today? (1-2): "; cin >> numPlayers; cin.ignore(); }while (numPlayers != 1 && numPlayers != 2); numComputer = 2 - numPlayers; //inform player of who is playing if (numPlayers == 1) { cout << "\nGreat! So there will be " << numPlayers << " Human Vs. " << numComputer << " Computer!\n\n"; players[0] = "Player"; players[1] = "Computer"; } else { cout << "Great! So it will be Human Vs. Human!\n\n"; players[0] = "Player 1"; players[1] = "Player 2"; } cin.get(); //RULES! system("cls"); cout << "\t\tRULES: \n\n"; cout << "1. You must pick a position to start in. (1-9)\n" << "the top row is 1-3, second 4-6, third 7-9" << endl; cout << "\n2. Who goes 'first' is random....so no complaining!\n\n"; cin.get(); system("cls"); //Display who is first cout << "DECIDING WHO GOES FIRST...PLEASE WAIT....."; whoFirst = rand() % 2 + 1; //pick either one of the players to go first cin.get(); if(whoFirst == 1) { playerMove = true; cout << "\n\n" << players[0] << " goes first"; cin.get(); } else { playerMove = false; cout << "\n\n" << players[1] << " goes first"; cin.get(); } system("cls"); //now enter the game of tic-tac-toe do { gameBoardFull = true; system("cls"); //check to see if full board (draw) for(int j = 0; j < 3; j++) { for(int k = 0; k < 3; k++) { if (board[j][k] == "|#|") { gameBoardFull = false; } } } if(gameBoardFull == false) { //display board for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cout << board[i][j]; } cout << endl; } //check who's move it is and get input do { if( playerMove == true) { cout << players[0] << ": "; cin >> move; cin.ignore(); } else if(playerMove == false && numPlayers == 2) { cout << players[1] << ": "; cin >> move; cin.ignore(); } else if(playerMove == false && numPlayers == 1) { move = rand() % 9 + 1; } } while(move < 1 && move > 9); //adjust move to the corresponding row and columns if (move > 0 && move < 4) { rowSelect = 0; columnSelect = move - 1; } else if (move > 3 && move < 7) { rowSelect = 1; columnSelect = move - 4; } else if (move > 6 && move < 10) { rowSelect = 2; columnSelect = move - 7; } if(board[rowSelect][columnSelect] == "|#|") { //place the marker on board if (playerMove == true) { board[rowSelect][columnSelect] = "|X|"; playerMove = false; } else if (playerMove == false && numPlayers == 2) { board[rowSelect][columnSelect] = "|O|"; playerMove = true; cin.get(); } else { board[rowSelect][columnSelect] = "|O|"; playerMove = true; cout << players[1] << ": " << move; cin.get(); } } } //check for winner for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { } } }while(gameBoardFull == false);//add whoWin == 0 so that it will check for winner before end loop system("cls"); if(gameBoardFull == true) cout << "\t\tGame Ended in Draw!"; cin.get(); break; } cin.get(); }
Thanks again!



LinkBack URL
About LinkBacks



