Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE ANSWER TO C++ ONLY!! In this game, two players take turns to move their pieces on a 3 3 game board. Player1 holds pieces

PLEASE ANSWER TO C++ ONLY!!

In this game, two players take turns to move their pieces on a 3 3 game board. Player1 holds pieces A, B, C and Player2 hold pieces D, E, F. The initial state is shown in Figure (a). In each round, each player must move one of their pieces one step along the line on the board. However, the pieces can not move out of the board or overlap with other pieces. The player who first occupies three diagonal positions of the board will win the game. Figure (d) shows three examples that Player1 wins.

image text in transcribed

We have implemented a code skeleton for this game (code), in which some parts are left blank with essential comments. You can start from the provided code and fill in all the blanks with TODOs. You can also implement it from scratch by yourself.

  • As shown in Figure (b), the coordinates of each position are described by two integer variables, which indicates the row and column respectively.
  • The player can move a piece from one position to another position by selecting one of eight directions, which are denoted by number 0-7 as shown in Figure (c). However, the player can make the move only when:
    • There is a direct line between the source position and target position as shown in Figure (a).
    • No other piece is on the target position.
  • In each round, the program should prompt the player to select a piece (A-F) and the direction to move (0-7). You must perform input validation:
    • The piece must be A, B or C for Player1 and D, E or F for Player2.
    • The movement must be between 0-7.
    • There is a line between the original position to the target position.
    • After moving the piece can not be out of the board or overlapped with other pieces.
  • You should ask the player to input again if the input is invalid. Note:
    • You can assume the input is always non-empty and contains one character only, i.e., 'A', 'D' and '3', but not 'AB', 'COMP2011'.
    • You can assume the input is always a character when selecting a piece, and a digit when selecting the direction to move.
    • Sometimes the selected piece can not move to any direction or all pieces of a player can not move. But you don't need to handle this case.
  • There are two players, Player1 and Player2. Player 1 always starts first.
  • End the game if one player wins after moving a piece.
#include  using namespace std; int main(){ // Initializing variables to store the positions of all pieces. unsigned int A_x = 0, A_y = 0; unsigned int B_x = 1, B_y = 0; unsigned int C_x = 2, C_y = 0; unsigned int D_x = 0, D_y = 2; unsigned int E_x = 1, E_y = 2; unsigned int F_x = 2, F_y = 2; // Initializing other variables unsigned int round = 1; // The round number of the game. char piece = ' '; // Variable to store which piece the player select. unsigned int direction = 0; // Variable to store which direction the player select. cout   (0, 1) (0,0) (0, 2) 0 (1, 1) B (1,0)  (1,2) 7 A 3 (2,0) (2, 1) (2, 2) 5 (a) Initial state of the game (b) Positions on the game board (c) Codes of movements A A B B (d) Example cases that player1 wins

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions