Tic Tac Toe Programming help C++ -
my main questions are, how tokens (x's , o's) appear on game board? have ask user row number , column number , token placed there. may able tell, new programming. have been @ hours , done. asking on here last resort. if of code done differently or more efficiently or isn't necessary or if did wrong, please let me know. out of ideas. finish program, more detailed be, better. appreciate it. confused , need advice finish this.
my code far:
#include <iostream> using namespace std; void createboard(int board[][3], char charboard[][3]); void printboard(char charboard[][3]); int checkwinner (int board[][3], int player); bool checkmove(int board[][3], int row, int column); void resetboard(int board[][3]); int main () { int board[3][3]; char charboard[3][3]; int player; int row, column; bool win = false; char again = 'y'; while (win == false) { createboard(board, charboard); printboard(charboard); cout << "player o's turn!" << endl; cout << "enter row number: "; cin >> row; cout << "enter column number: "; cin >> column; checkmove(board, row, column); //update board win = checkwinner(board, -1); if (checkwinner (board, -1) == true) { cout << "player o wins!" << endl; break; } //set flag denote it's x's turn printboard(charboard); cout << "player x's turn!" << endl; cout << "enter row number: "; cin >> row; cout << "enter column number: "; cin >> column; checkmove(board, row, column); //update board win = checkwinner (board, 1); if (checkwinner (board, 1) == true) { cout << "player x wins!" << endl; break; } cout << endl << endl; cout << "would play again? (y/n): "; cin >> again; } cout << endl << endl; cout << "good bye!" << endl; return 0; } void createboard(int board[][3], char charboard[][3]) { (int = 0; < 3; i++) { (int j = 0; j < 3; j++) { // if game board has -1, put "o" in character board if (board[i][j]== -1) { charboard[i][j] = 'o'; } // if game board has 1, put "x" in character board else if (board[i][j]== 1) { charboard[i][j] = 'x'; } // if game board has 0, put " " (blank space) in character board else { charboard[i][j] = ' '; } } } } void printboard(char charboard[][3]) { cout << " tic tac toe!" << endl << endl; cout << " column\n"; cout << " 0 1 2" << endl << endl; cout << "r 0 " << charboard[0][0] << " | " << charboard[0][1] << " | " << charboard[0][2] << endl; cout << " --------------------" << endl; cout << "o 1 " << charboard[1][0] << " | " << charboard[1][1] << " | " << charboard[1][2] << endl; cout << " --------------------" << endl; cout << "w 2 " << charboard[2][0] << " | " << charboard[2][1] << " | " << charboard[2][2] << endl << endl; } int checkwinner (int board[][3], int player) { int sum; // check each column winner (int = 0; < 3; i++) { sum = board[i][0] + board[i][1] + board[i][2]; // if o winner if (sum == 3*player) { return true; } } // check each row winner (int j = 0; j < 3; j++) { sum = board[0][j] + board[1][j] + board[2][j]; // if o winner if (sum == 3*player) { return true; } } // check , front diagonal sum = board[0][0] + board[1][1] + board[2][2]; if (sum == 3*player) { return true; } sum = board[0][2] + board[1][1] + board[2][0]; if (sum == 3*player) { return true; } return false; } bool checkmove(int board[][3], int row, int column) { if ((row < 0) || (row > 2)) { return false; } else if ((column < 0) || (column > 2)) { return false; } else if (board[row][column] == 0) { return true; } else { return false; } } void resetboard(int board[][3]) { (int = 0; < 3; i++) { (int j = 0; j < 3; j++) { board[i][j] = 0; } } }
it looks should insert appropriate value in 'checkmove' function:
else if (board[row][column] == 0) { board[row][column] = value; // value -1 or 1 return true; }
where 'value' either -1 or 1 depending on if inserting x or o. function doesn't have information on adding mark, may add parameter function pass in 'value'.
Comments
Post a Comment