Question

c++ help please!

  1. Create a 2D character array in your main function and use nested for loops to fill the array with the letter ‘e’ to represent empty spaces.

  2. Create a function to print the board on the screen using a nested for loop. The function header is:
    void printBoard (char board [][3])

  3. Create a function that checks whether a particular space has already been filled. If the space is filled it returns a boolean value of true, otherwise false. (No loops required.) Here is the function header:
    bool isFilled (char board [][3], int row, int col)

  4. Write a function to prompt the user for their next move. The function should validate that the move is legal and then update the board to reflect the move (hint: arrays are always passed by reference). A move is valid if the values of row and col are both numbers between 0 and 2 and the space is not already filled (hint: you have a function to do part of this). The parameter player will be ‘X’ if the function is to get X’s move and ‘O’ if it is to get O’s move. The function header is:
    void move (char board [][3], char player)

  5. Demo your program for me … you’ll need to have main() modify the board and call your functions for testing purposes. Don’t move on until you get credit for the required part of the lab!

  6. Create a function to detect whether it is game over: either someone has won or a stalemate has been reached. If you’re short on time, you could just ask the user. If you have enough time, test to see whether any of the rows, columns or diagonals contains all Xs or all Os and print out a congratulatory message. Return true if the function finds a winner or if the board is completely full and false otherwise. The function header is:
    bool isGameOver(char board[][3])

  7. Put all the function calls together in main ( ) and alternate getting a move from player X and player O until someone wins.

C C:\Windows\system32\cmd.exe Player 0: please enter the coordinates of your nove (row col): 1 8 X e e ре е e e e Player X: p

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Code [C++]

#include <iostream>
using namespace std;

char player = 'O';
  
// printBoard function

void printBoard(char board[][3]) {
  
   for (int i=0; i<3; i++) {
       for (int j=0; j<3; j++)
           cout << board[i][j] << " ";
       cout << endl;
   }
}

// isFilled function

bool isFilled(char board[][3], int row, int col) {
  
   // Determine whether particular space is occupied or not
  
   if(board[row][col] != 'e')
       return true;
   else
       return false;
}

// move function

void move(char board[][3], char player) {
  
   // Asking user for their move
  
   cout << "Player " << player << ": please enter the coordinates of your move <row col>: ";
   int row, col;
   cin >> row >> col;
  
   while(!((row >=0 && row <=2) && (col >=0 && col <= 2) && !(isFilled(board, row, col)))) {

       cout << "Invalid move! That cell is already taken!" << endl;
       cout << "\nPlayer " << player << ": please enter the coordinates of your move <row col>: ";
       cin >> row >> col;
   }
  
   board[row][col] = player;
}

// isGameOver function

bool isGameOver(char board[][3]) {

   // Testing whether any rows, cols or diagonals contain all Xs or Os
  
   // Checking for all rows

   for (int i=0; i<3; i++)
       if((board[0][i] == board[1][i]) && (board[1][i] == board[2][i]) &&
           (board[0][i] == board[2][i]) && board[0][i] != 'e') {
               cout << "Player " << player << " has won!" << endl;
               return true;
           }
      
   // Checking for same rows

   for (int i=0; i<3; i++)
       if((board[i][0] == board[i][1]) && (board[i][1] == board[i][2]) &&
           (board[i][0] == board[i][2]) && board[i][0] != 'e') {
               cout << "Player " << player << " has won!" << endl;
               return true;
           }

   // Checking for diagnoals
   if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2]) && (board[0][0] == board[2][2]) && board[0][0] != 'e') {
       cout << "Player " << player << " has won!" << endl;
       return true;
   }

   if((board[2][0] == board[1][1]) && (board[1][1] == board[0][2]) && (board[2][0] == board[0][2]) && board[2][0] != 'e') {
       cout << "Player " << player << " has won!" << endl;
       return true;
   }
              
   // Checking whether board is filled or not

   for (int i=0; i<3; i++)
       for (int j=0; j<3; j++)
           if(!isFilled(board, i, j)) {
               return false;
           }
          
   return true;
}

int main() {

   // Creating 2D Array
  
   char board[3][3];
  
   // Using nested loop to fill array with letter e
  
   for (int i=0; i<3; i++) {
      
       for (int j=0; j<3; j++)
           board[i][j] = 'e';
   }

   while(!isGameOver(board)) {
      
       if(player == 'X')
           player = 'O';
       else
           player = 'X';
      
       move(board, player);
      
       printBoard(board);
   }
  
   return 0;
}

Sample Output:-

C:\Users\Dell OneDrive\Desktop\OCT 2020\Board.exe Player x: please enter the coordinates of your move <row cols: 0 0 хе е е е

----------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES!!!
HIT A THUMBS UP IF YOU DO LIKE IT!!!

Add a comment
Know the answer?
Add Answer to:
c++ help please! Create a 2D character array in your main function and use nested for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { ...

    #include <stdio.h> #include <stdlib.h> #include <string.h> struct game_piece { }; struct game_board { }; void game_piece_init_default(struct game_piece* piece) { } void game_piece_init(struct game_piece* piece, char* new_label) { } char* game_piece_get_label(struct game_piece* piece) { return ""; } char* game_piece_to_string(struct game_piece* piece) { return ""; } void game_board_init(struct game_board* game_board, int rows, int cols) { } int game_board_is_space_valid(struct game_board* game_board, int row, int col) { return 0; } int game_board_add_piece(struct game_board* game_board, struct game_piece* piece, int row, int col) { return 0;...

  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use...

    Tic-Tac-Toe Game Write a program that allows two players to play a game of tic-tac-toe. Use a two-dimensional char 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: Displays the contents of the board array. Allows player 1 to select a location on the board for an X. The program should ask the user to enter...

  • Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game....

    Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLE...

    Write a class named FBoard for playing a game... PLEASE USE C++ PLEASE DO NOT USE "THIS -->". NOT ALLOWED PLEASE PROVIDE COMMENTS AND OUTPUT! Write a class named FBoard for playing a game, where player x is trying to get her piece to row 7 and player o is trying to make it so player x doesn't have any legal moves. It should have: An 8x8 array of char for tracking the positions of the pieces. A data member...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • Please develop the following code using C programming and using the specific functions, instructi...

    Please develop the following code using C programming and using the specific functions, instructions and format given below. Again please use the functions given especially. Also don't copy any existing solution please write your own code. This is the first part of a series of two labs (Lab 7 and Lab 8) that will complete an implementation for a board-type game called Reversi (also called Othello). The goal of this lab is to write code that sets up the input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT