In Visual Basic Code. Design a program that allows two players to play a game of
ID: 3690020 • Letter: I
Question
In Visual Basic Code. Design a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. c. Allows player 2 to select a location on the board for an O. The program should ask the user to eter the row and column number. d. Determines whether a player has won or if a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. e. Player 1 wis when there are three X's in a row on the game board. Player 2 wins when there are three O's in a row on the game board. The winning X's or O's can appear in a row, in a column, or diagonally across the board. A tie occurs when all of the locations on the board are full, but there is no winner.
Explanation / Answer
Edit & Run
#include<iostream> #include<cstdlib> using namespace std; class TicTacToe{ public: void print(); void play(); char determine(); TicTacToe(); private: int p1x,p1y; int p2x,p2y; char TicTacToeBoard[3][3]; }; TicTacToe::TicTacToe(){ char TicTacToeBoard[3][3] = {{'!','!','!'},{'!','!','!'},{'!','!','!'} }; // fill board TicTacToeBoard[p1x][p1y] = 'X'; // put x where p1 has coordinates TicTacToeBoard[p2x][p2y] = 'O'; // put y where p1 has coordinates } int main(){ TicTacToe playgame,winner; //create objects of type TicTacToe playgame.play(); // play game winner.determine(); // determine winner system("pause"); return 0; } void TicTacToe::print(){ // for printing board int row; int col; cout << "----------------------" << endl; for(row = 0;row < 3; row++){ for(col = 0;col < 3; col++){ cout << TicTacToeBoard[row][col]; } cout << endl; } cout << "----------------------" << endl; } void TicTacToe::play(){ // play game TicTacToe showboard; int turn = 1; while(turn <= 9){ // let player 1 enter coordinates cout << "Player 1" << endl; cout << "X:"; cin >> p1x; cout << "Y:"; cin >> p1y; showboard.print(); // print board cout << endl; turn++; // let player 2 enter coordinates cout << "Player 2" << endl; cout << "X:"; cin >> p2x; cout << "Y:"; cin >> p2y; showboard.print(); // print board cout << endl; turn++; } } char TicTacToe::determine(){ // determine the winner might be incorrect if( TicTacToeBoard[0][0]==TicTacToeBoard[0][1] && TicTacToeBoard[0][1] == TicTacToeBoard[0][2] ){ return TicTacToeBoard[0][0]; } else if( TicTacToeBoard[1][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[0][2] ){ return TicTacToeBoard[1][0]; } else if( TicTacToeBoard[2][0]==TicTacToeBoard[2][1] && TicTacToeBoard[2][1] == TicTacToeBoard[0][2] ){ return TicTacToeBoard[2][0]; } else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][0] && TicTacToeBoard[1][0] == TicTacToeBoard[2][0] ){ return TicTacToeBoard[0][0]; } else if( TicTacToeBoard[0][1]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][1] ){ return TicTacToeBoard[0][1]; } else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][2] && TicTacToeBoard[1][2] == TicTacToeBoard[2][2] ){ return TicTacToeBoard[0][2]; } else if( TicTacToeBoard[0][0]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){ return TicTacToeBoard[0][0]; } else if( TicTacToeBoard[0][2]==TicTacToeBoard[1][1] && TicTacToeBoard[1][1] == TicTacToeBoard[2][2] ){ return TicTacToeBoard[0][2]; } else return ' '; } Edit & Run
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.