Specify, Design, and implement a class that can be one player in a game of tic-t
ID: 3789645 • Letter: S
Question
Specify, Design, and implement a class that can be one player in a game of tic-tac-toe. The constructor should specify whether the object is to be the first player (X's) or the second player (O's). There should be a memeber function to ask the object to make its next move, and a member function that tells the object what the opponent's next move is.Also include other useful member functions, such as a function to ask whether a given spot of the tic-tac-toe board is occupied, and if so, whether the occupation is with an X or an O. Also a member function to determine when the game is over, and whether it was a draw, an X win, or an O win.
Use the class in two programs: a program that plays tic-tac-toe against the program's user, and a program that has two tic-tac-toe objects that play against each other.
//tictactoe.h
#pragma once #include #include /* printf, NULL */ #include /* srand, rand */ #include using namespace std; class TicTacToePlayer { public: TicTacToePlayer(int FirstPlayer); TicTacToePlayer(); ~TicTacToePlayer(); /* prints the board */ void printBoard(); /* user made move */ void makeMove(int); /*alternates moves between players*/ void switchPlayer(); /* computer made move */ void automateMove(int, int); /* checks if position in the board is ocupied, returns 1 for occupied 0 for not occupied */ int checkPos(int); /* checks and prints the status of game, with the help of checkWin() retuns 0 for inprogress 1 for end of game */ int checkGameStatus(); private: /* Use an appropriate data structure for storing the board */ char pos[10] = {'o', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; char symbol; /* Checks who won returns 0 for X win 1 for O win 2 for draw */ int checkWin(); };
//implemenation file tictactoe.cpp
#include "ticTacToe.h" #include /* printf, NULL */ #include /* srand, rand */ #include TicTacToePlayer::TicTacToePlayer(int FirstPlayer) { if (FirstPlayer) symbol = 'X'; else symbol = 'O'; } TicTacToePlayer::TicTacToePlayer() { srand(time(0)); } TicTacToePlayer::~TicTacToePlayer() { } void TicTacToePlayer::printBoard() { cout << " |" << " |" << endl; cout << " " << pos[1] << " | " << pos[2] << " | " << pos[3] << endl; cout << " _______|" << "_______|_______" << endl; cout << " |" << " |" << endl; cout << " " << pos[4] << " | " << pos[5] << " | " << pos[6] << endl; cout << " _______|" << "_______|_______" << endl; cout << " |" << " |" << endl; cout << " " << pos[7] << " | " << pos[8] << " | " << pos[9] << endl; cout << " |" << " |" << endl; } void TicTacToePlayer::makeMove(int row) { do { pos[row] = symbol; } while (row < '9' || row >= '0'); } void TicTacToePlayer::switchPlayer() { if (symbol == 'X') symbol = 'O'; else symbol = 'X'; } void TicTacToePlayer::automateMove(int row, int num) { //row = num; if (symbol == 'X') symbol = 'O'; //else symbol = 'X'; pos[row] = symbol; //} while (row < 0 || row > 8 || row != 0); } int TicTacToePlayer::checkPos(int row) { if (row == '1' && pos[1] == '1') { pos[1] = symbol; return 1; } else if (row == '2' && pos[2] == '2') { pos[2] = symbol; return 1; } else if (row == '3' && pos[3] == '3') { pos[3] = symbol; return 1; } else if (row == '4' && pos[4] == '4') { pos[4] = symbol; return 1; } else if (row == '5' && pos[5] == '5') { pos[5] = symbol; return 1; } else if (row == '6' && pos[6] == '6') { pos[6] = symbol; return 1; } else if (row == '7' && pos[7] == '7') { pos[7] = symbol; return 1; } else if (row == '8' && pos[8] == '8') { pos[8] = symbol; return 1; } else if (row == '9' && pos[9] == '9') { pos[9] = symbol; return 1; } else { cout << "Invalid Move, make another one:" << endl; return 0; } } int TicTacToePlayer::checkGameStatus() { checkWin(); return 0; } int TicTacToePlayer::checkWin() { bool end(false); if (pos[2] == pos[1] && pos[1] == pos[3]) { cout << "Player " << symbol << " wins." << endl; end = true; } else if (pos[5] == pos[4] && pos[6] == pos[4]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[8] == pos[7] && pos[9] == pos[7]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[4] == pos[1] && pos[7] == pos[1]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[5] == pos[2] && pos[8] == pos[2]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[6] == pos[3] && pos[9] == pos[3]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[5] == pos[1] && pos[9] == pos[1]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[4] == pos[2] && pos[6] == pos[2]) { cout << "Player " << symbol << " wins." << endl; } else if (pos[5] == pos[3] && pos[7] == pos[3]) { cout << "Player " << symbol << " wins." << endl; } return 0; }
main.cpp
#include #include "ticTacToe.h" #include /* printf, NULL */ #include /* srand, rand */ #include using namespace std; int main() { srand(time(0)); int FirstPlayer; TicTacToePlayer player1(FirstPlayer = 1); TicTacToePlayer player2(FirstPlayer = 0); char row = ' '; int num = 0; char symbol = ' '; cout << "Enter 1 if you want to be the 1st player or 0 for 2nd player:" << endl; cin >> FirstPlayer; cout << "yes" << endl; player1.printBoard(); if (FirstPlayer) { while (1) { do { cout << "Player " << FirstPlayer << " enter your mark: " << endl; cin >> row; player1.checkPos(row); player1.printBoard(); player1.checkGameStatus(); } while (player1.checkPos(row)); cout << "Player " << FirstPlayer << " enter your mark: " << endl; cin >> row; if (player1.checkGameStatus()) { break; } system("pause"); cout << "no" << endl; player1.printBoard(); num = rand() % 9; cout << endl; int row = (int)num; player2.automateMove(row, num); player2.checkPos(row); cout << "Player " << FirstPlayer << " marked row " << row << endl; player2.printBoard(); if (player2.checkGameStatus()) { break; } cout << "board" << endl; player1.printBoard(); } } else { while (1) { player1.checkPos(row); //player.checkGameStatus(); if (player1.checkGameStatus()) { break; } //player.printBoard(); do { num = rand() % 9; //cout << "num us==" << num << endl; cout << endl; int row = (int) num; player1.automateMove(row, num); player1.checkPos(row); cout << "Player " << FirstPlayer << " enter your mark: " << endl; cin >> (int) row; //flag = player.checkPos(row); } while (player1.checkPos(row)); //cout << "Player " << isFirstPlayer << " enter your mark: " << endl; //cin >> row; if (player1.checkGameStatus()) { break; } player1.printBoard(); }system("pause"); } return 0; }
i wrote a program but im in need of some assistance on finishing it.
Explanation / Answer
#include "ticTacToe.h"
#include<iostream.h>
#include "MainPage.g.h"
#include "..Commongame.h"
namespace TicTacToeWinRT
{
public ref class MainPage sealed
{
private:
tictactoe_game game;
};
}
void TicTacToeWinRT::MainPage::btnStartUser_Click(Object^ sender, RoutedEventArgs^ e)
{
ResetGame();
game.start(tictactoe_player::user);
}
void TicTacToeWinRT::MainPage::btnStartComputer_Click(Object^ sender, RoutedEventArgs^ e)
{
ResetGame();
game.start(tictactoe_player::computer);
auto cell = game.move(tictactoe_player::computer);
PlaceMark(cell, tictactoe_player::computer);
}
void TicTacToeWinRT::MainPage::PlaceMark(tictactoe_cell const cell, tictactoe_player const player)
{
auto image = ref new Image();
auto bitmap = ref new BitmapImage
(
ref new Uri(player == tictactoe_player::computer ? "ms-appx:///Assets/tttx.bmp" : "ms-appx:///Assets/ttt0.bmp"));
bitmap->ImageOpened += ref new RoutedEventHandler
(
[this, image, bitmap, cell](Object^ sender, RoutedEventArgs^ e)
{
image->Width = bitmap->PixelWidth;
image->Height = bitmap->PixelHeight;
image->Visibility = Windows::UI::Xaml::Visibility::Visible;
});
image->Source = bitmap;
image->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
image->HorizontalAlignment = Windows::UI::Xaml::HorizontalAlignment::Center;
image->VerticalAlignment = Windows::UI::Xaml::VerticalAlignment::Center;
Grid::SetRow(image, cell.row);
Grid::SetColumn(image, cell.col);
board->Children->Append(image);
}
void TicTacToeWinRT::MainPage::ResetGame()
{
std::vector<Windows::UI::Xaml::UIElement^> children;
for(auto const & child : board->Children)
{
auto typeName = child->GetType()->FullName;
if(typeName == "Windows.UI.Xaml.Controls.Image" ||
typeName == "Windows.UI.Xaml.Shapes.Line")
{
children.push_back(child);
}
}
for(auto const & child : children)
{
unsigned int index;
if(board->Children->IndexOf(child, &index))
{
board->Children->RemoveAt(index);
}
}
txtStatus->Text = nullptr;
}
void TicTacToeWinRT::MainPage::board_PointerReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
if(game.is_started() && ! game.is_finished())
{
auto cellw = board->ActualWidth / 3;
auto cellh = board->ActualHeight / 3;
auto point = e->GetCurrentPoint(board);
auto row = static_cast<int>(point->Position.Y / cellh);
auto col = static_cast<int>(point->Position.X / cellw);
game.move(tictactoe_cell(row, col), tictactoe_player::user);
PlaceMark(tictactoe_cell(row, col), tictactoe_player::user);
if(!game.is_finished())
{
auto cell = game.move(tictactoe_player::computer);
PlaceMark(cell, tictactoe_player::computer);
if(game.is_finished())
{
DisplayResult(
game.is_victory(tictactoe_player::computer) ?
tictactoe_player::computer :
tictactoe_player::none);
}
}
else
{
DisplayResult
(
game.is_victory(tictactoe_player::user) ?
tictactoe_player::user :
tictactoe_player::none);
}
}
}
void TicTacToeWinRT::MainPage::DisplayResult(tictactoe_player const player)
{
Platform::String^ text = nullptr;
switch (player)
{
case tictactoe_player::none:
text = "It's a draw!";
break;
case tictactoe_player::computer:
text = "Computer wins!";
break;
case tictactoe_player::user:
text = "User wins!";
break;
}
txtStatus->Text = text;
if(player != tictactoe_player::none)
{
auto coordinates = game.get_winning_line();
if(coordinates.first.is_valid() && coordinates.second.is_valid())
{
PlaceCut(coordinates.first, coordinates.second);
}
}
}
void TicTacToeWinRT::MainPage::PlaceCut(tictactoe_cell const start, tictactoe_cell const end)
{
auto cellw = board->ActualWidth / 3;
auto cellh = board->ActualHeight / 3;
auto line = ref new Line();
line->X1 = start.col * cellw + cellw / 2;
line->Y1 = start.row * cellh + cellh / 2;
line->X2 = end.col * cellw + cellw / 2;
line->Y2 = end.row * cellh + cellh / 2;
line->StrokeStartLineCap = Windows::UI::Xaml::Media::PenLineCap::Round;
line->StrokeEndLineCap = Windows::UI::Xaml::Media::PenLineCap::Round;
line->StrokeThickness = 15;
line->Stroke = ref new SolidColorBrush(Windows::UI::Colors::Red);
line->Visibility = Windows::UI::Xaml::Visibility::Visible;
Grid::SetRow(line, 0);
Grid::SetColumn(line, 0);
Grid::SetRowSpan(line, 3);
Grid::SetColumnSpan(line, 3);
board->Children->Append(line);
}
output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.