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

I have this issue with my program Everything works good but when I run it it onl

ID: 3825933 • Letter: I

Question

I have this issue with my program

Everything works good but when I run it it only shows Player 1

But what it should do is after Player 1 turn it should switch to Player 2.

THE QUESTION IS ASKING:

Using Classes

1.Define the functions declared in TicTacToe.h so that main.cpp produces results similar to when .obj is included in the project

Three files are provided to you.

Main.cpp contains the driver that will run the declared functions to ensure they work properly, it does not need to be altered or submitted.

.h contains the declarations and function prototypes related to the class, it does not need to be altered or submitted.

.obj contains a compiled solution that will allow you to test existing functionality. It needs to be removed from your project for you to test your own implementation. This file is not needed to complete the project, I will demo the program at the beginning of the lab session.

Functions to be defined:

//Returns 1 or 2 if the respective player has won, 0 if there is no winner, or 3 if the game ends without a winner

int checkWin();

//Default constructor, begins game with an empty board

TicTacToe();

//Displays the current state of the board

void displayBoard();

//Returns -1 to indicate an invalid move, or the results of checkWin if the move is valid

int makeMove(int position);

Class variables:

//Board stores a 0 to indicate an empty position, or 1/2 if a player is occupying it

int board[3][3];

//Used to indicate if the next move will be performed by player 1 or player 2

int nextPlayer;

Submission:

You will submit a file named <Last Name><First Initial>labFinal.cpp that contains all necessary function definitions to make main.c work when your file is included in the project.

For example, my assignment would be named MayElabFinal.cpp

Main Function:

//Eric May
//CPSC 121 Final Lab
//5/8/2016
#include "TicTacToe.h"
#include <iostream>

int main()
{
   //The instance of our class that will be used
   TicTacToe game;
   int res = 0;
   do {
       //Tell user if the last move was invalid
       if (res == -1)
           std::cout << "Invalid position. Try again. ";
       //Show current game state
       game.displayBoard();
       //Prompt user for desired move
       std::cout << "Player " << game.nextPlayer << "'s turn. Enter 0-8: ";
       int pos;
       std::cin >> pos;
       //Attempt to make provided move
       res = game.makeMove(pos);
   } while (res <= 0); //While game isn't over
   if (res == 3)//Cat's game
       std::cout << "Nobody wins! ";
   else//Victory achieved
       std::cout << "Player " << res << " has won! ";
   system("pause");
   return 0;
}

TicTacToe.h Function:

//Eric May
//CPSC 121 Final Lab
//5/8/2016
#pragma once

//Contains methods allowing for a user to play a game of tic-tac-toe
class TicTacToe {
private:
   //Board stores a 0 to indicate an empty position, or 1/2 if a player is occupying it
   int board[3][3];
   //Returns 1 or 2 if the respective player has won, 0 if there is no winner, or 3 if the game ends without a winner
   int checkWin();
public:
   //Used to indicate if the next move will be performed by player 1 or player 2
   int nextPlayer;
   //Default constructor, begins game with an empty board
   TicTacToe();
   //Note: Since we aren't dynamically allocating memory, there's no need for a deconstructor
   //Displays the current state of the board
   void displayBoard();
   /*   Returns -1 to indicate an invalid move, or the results of checkWin if the move is valid
       Board Position Mapping
           0   1   2
           3   4   5
           6   7   8
       Use 1-9 if that's easier for you, but make sure you document it!   */
   int makeMove(int position);
};

#include "TicTacToe .h" #include Kiostream> #include Kcstdlib> include ctime.h> using namespace std; TicTacToe:: TicTacToe() //empty board nextPlayer 1; for (int i e; ic3; i++) for (int j 0; j

Explanation / Answer

in makeMove method, nextplayer variable should update, which was missing..

Replace the makemove method like this,,so that it will start work fine...

int TicTacToe::makeMove(int position)
{
   if((position<0) || (position>8))
       return -1;
   int i = position/3;
   int j = position%3;
   board[i][j] = nextplayer;
   if(nextplayer == 1){
       nextplayer = 2;
   }
   else{
       nextplayer = 1;
   }
   return checkWin();
}