use visual studio and Write a c++ program that allows two different people to pl
ID: 3717795 • Letter: U
Question
use visual studio and Write a c++ program that allows two different people to play tic-tac-toe.
Here are the programming guidelines:
-The Modular programming Technique MUST BE USED! What does modular programming mean? You must break youe code down into specific "chunks" or "block" of functions that can be called throughout the program. For example, you may want to have a function that updates the board display. Another function to get input, another function to check for a winner. You decide how many functions and what each will do.
- Data structure: a [3][3] array must be used for the data.
-winner state: use a boolean variable called winner to track if the game has a winner. set the variable to false at the beggining of the program and when a user gets 3 in a row, set it to true .
- Graphics and User Interaction: Because you have not learned graphics command in C++, you will need to do your best with how the game board look. You can use chars of "X" and "Y" or use integers 1 and 2 to represent X & Y respectfully. 0 will represent a blank square. The playing board should be displayed on the screen and be updated after each player turn. At a minimum the board on the screen should look like:
000
000
000
As the players play, the board should look like:
012
102
100
- User Input: Input player's name (instead of using player1 or player2). Each user will select which square to select by designating a row and column. For example, if the user wants to select the top right corner, the row is 1, column 3. If the user wants to select the bottom middle square, the row is 3 and the column is 2.
Error Checking: all user input must be error checked. Meaning, is a user input inputs row 4 and column 0, the program should ask the user to input valid inputs.
comments in the code: Yes
The winner: When a player wins with 3 across, 3 down or 3 diagonal, the game should stop and display who the winner is.If neither one wins, then the "cat" gets the win.
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
int board[3][3]; // our tic tac toe board
string player1; // player1's name
string player2; // player2's name
// this function initiates the game
void init()
{
memset(board, 0, sizeof board); // sets all elements of board to zero
cout<<"Enter player1's name"<<endl;
cin>> player1;
cout<< "Enter player2's name"<<endl;
cin>>player2;
}
// check_error return 0 if coordinates are out of bound
// return 1 if player attempts to play in already played cell
// return 2 if move is valid
int check_error(int row,int col)
{
if(row>3||row<1||col>3||col<1)
return 0;
else if(board[row-1][col-1]!=0)
return 1;
else
return 2;
}
// displays board
void display_board(void)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<board[i][j]<<" ";
cout<<endl;
}
}
// updates the board
void update_board(int row,int col,int turn)
{
board[row-1][col-1]=turn;
display_board();
}
void input(string player,int turn)
{
// keep taking input will exit the loop once player enter's the correct coordinates
while(true)
{
cout<<player<<", Enter row and column to make your move"<<endl;
int row,col;
cin>>row>>col;
int flag= check_error(row,col);
if(flag==0)
cout<<"Invalid coordinates, enter again"<<endl;
else if(flag==1)
cout<<"Cannot make move in this position, it is already used, enter again"<<endl;
else if(flag==2)
{
update_board(row,col,turn);
return;
}
}
}
int check_state(int turn)
{
for(int i=0;i<3;i++)
{
//check for rows
if(turn==board[i][0] && board[i][0]==board[i][1] && board[i][1]==board[i][2])
return 1;
//check for columns
if(turn==board[0][i] && board[0][i]==board[1][i] && board[1][i]==board[2][i])
return 1;
}
//check for diagnols
if(turn==board[0][0]&&board[0][0]==board[1][1] && board[1][1]==board[2][2])
return 1;
if(turn==board[0][2] && board[0][2]==board[1][1] && board[1][1]==board[2][0])
return 1;
// check if game is a draw by checking if there is a 0 or not
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
return 2;
}
}
return 0;
}
int main(void)
{
init();
int turn=1; // tells us about which player has to move
bool winner=false;
//status =0 represents game is drawn
//status =1 represents game is won by somebody
// status=2 game is not won and play can still go on
int status=2;
int name_winner; // stores who is the winner(0 or 1)
while(status==2)
{
if(turn==1)
{
input(player1,turn);
status=check_state(turn);
if(status==1)
name_winner=turn;
turn=2;
}
else if(turn==2)
{
input(player2, turn);
status=check_state(turn);
if(status==1)
name_winner=turn;
turn=1;
}
}
if(status==0)
cout<<"Game Drawn!!"<<endl;
else
{
winner=true;
if(name_winner==1)
cout<<"Winner is 1 i.e. "<<player1<<endl;
else
cout<<"Winner is 2 i.e. "<< player2<<endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.