Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Microsoft VISUAL BASIC computer language... I\' ve already posted this questi

ID: 3690036 • Letter: I

Question

In Microsoft VISUAL BASIC computer language... I've already posted this question TWICE. Requesting VISUAL BASIC. and gotten it answered wrong.

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 enter 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 wins when there are three Xs in a row on the game board. Player 2 wins when there are three Os in a row on the game board. The winning Xs or Os can appear in a row, in a column, or diagonally across the board. A tie occurs when all the locations on the board are full, but there is no winner.

Explanation / Answer

#include <iostream>

using namespace std;

void printTitle();
void showGame();
void setBoard();
void startGame();
void setPosition();
void printOptions();
bool checkWinner();

const int ROW = 3, COL = 3;
char board[ROW][COL];
int position[2];

int main(){

   printTitle();
   startGame();

   return 0;
}

void printTitle(){

   cout << " ********************";
   cout << " Tic-Tac-Toe Game   |";
   cout << " ********************";

}

void showGame(){

   cout << " ";

   for(int i = 0; i <= 2; i++){
       cout << " ";
       for(int j = 0; j <= 2; j++){
           cout << board[i][j];
       }
   }

   cout << " ";

}

void setBoard(){

   for(int i = 0; i <= 2; i++){
       for(int j = 0; j <= 2; j++){
           board[i][j] = '*';
       }
   }
}

void startGame(){
   bool winner = false;

   setBoard();
   while(!winner){
       for(int i = 2; i < 11; i++){
           if(i % 2 == 0){
               cout << " ----Player 1----";
               showGame();
               setPosition();
               board[position[0] - 1][position[1] - 1] = 'O';
               winner = checkWinner();
               if(winner){
                   cout << " Player 1 is the winner!";
                   break;
               }
           }else{
               cout << " ----Player 2----";
               showGame();
               setPosition();
               board[position[0] - 1][position[1] - 1] = 'X';
               winner = checkWinner();
               if(winner){
                   cout << " Player 2 is the winner!";
                   break;
               }
           }
           if(i == 10)winner = true;
       }
   }
   showGame();
}

void setPosition(){

   cout << "    Row: ";
   cin >> position[0];
   cout << "   col: ";
   cin >> position[1];

   while(position[0] < 0 || position[0] > 3 ||
       position[1] < 0 || position[1] > 3){
       cout << " Enter a valid position.";
       cout << "    Row: ";
       cin >> position[0];
       cout << "   col: ";
       cin >> position[1];
   }
  
   while(board[position[0] - 1][position[1] - 1] == 'X' ||
       board[position[0] - 1][position[1] - 1] == 'O'){
       cout << " Someone already went there. Try again.";
       cout << "    Row: ";
       cin >> position[0];
       cout << "   col: ";
       cin >> position[1];
   }
  
}

bool checkWinner(){

if(board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X')return true;
if(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X')return true;
if(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')return true;
if(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X')return true;
if(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X')return true;
if(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X')return true;
if(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X')return true;
if(board[2][0] == 'X' && board[1][1] == 'X' && board[0][2] == 'X')return true;

if(board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O')return true;
if(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O')return true;
if(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')return true;
if(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O')return true;
if(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O')return true;
if(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O')return true;
if(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O')return true;
if(board[2][0] == 'O' && board[1][1] == 'O' && board[0][2] == 'O')return true;

   return false;
}

sample output

********************                                                                                                                                        
Tic-Tac-Toe Game   |                                                                                                                                        
********************                                                                                                                                        
                                                                                                                                                            
----Player 1----                                                                                                                                            
                                                                                                                                                            
***                                                                                                                                                       
***                                                                                                                                                       
***                                                                                                                                                       
                                                                                                                                                            
   Row: 3                                                                                                                                                   
   col: 3                                                                                                                                                   
                                                                                                                                                            
                                                                                                                                                            
----Player 2----                                                                                                                                            
***                                                                                                                                                       
***                                                                                                                                                       
**O                                                                                                                                                       
                                                                                                                                                            
   Row: 1                                                                                                                                                   
   col: 2                                                                                                                                                   
                                                                                                                                                            
                                                                                                                                                            
----Player 1----                                                                                                                                            
                                                                                                                                                            
*X*                                                                                                                                                       
***                                                                                                                                                       
**O                                                                                                                                                       
                                                                                                                                                            
   Row: 2                                                                                                                                                   
   col: 2                                                                                                                                                   

----Player 2----                                                                                                                                            
                                                                                                                                                            
*X*                                                                                                                                                       
*O*                                                                                                                                                       
**O                                                                                                                                                       
                                                                                                                                                            
   Row: 1                                                                                                                                                   
   col: 2                                                                                                                                                   
                                                                                                                                                            
        Someone already went there. Try again.                                                                                                              
   Row: 1                                                                                                                                                   
   col: 1                                                                                                                                                   
                                                                                                                                                            
                                                                                                                                                            
----Player 2----                                                                                                                                            
                                                                                                                                                            
*X*                                                                                                                                                       
*O*                                                                                                                                                       
**O                                                                                                                                                       
                                                                                                                                                            
   Row: 1                                                                                                                                                   
   col: 2                                                                                                                                                   
                                                                                                                                                            
        Someone already went there. Try again.                                                                                                              
   Row: 1                                                                                                                                                   
   col: 1                                                                                                                                                    

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote