patterncppMinor
TicTacToe Implementation With Classes
Viewed 0 times
withtictactoeimplementationclasses
Problem
This is my first program using a class that I personally designed. Please pay special attention to the design of the class and I would love pointers and advice on how to better improve logical design of classes (what should be grouped with them, better ways of creating classes to interact with each other, etc).
I am also aware that there are constructors and deconstructors, but i'm unsure when I should use those or how they should be implemented. Advice for those would be helpful as well! I recently decided to stop using
Main
Class Implementation
```
//TicTacToe class implementation
//Leeroy Jenkins
#include "TicTacToeClass.h"
#include
bool TicTacToe::getUserWantToPlay()
{
char response;
bool invalidResponse = true;
bool play = fal
I am also aware that there are constructors and deconstructors, but i'm unsure when I should use those or how they should be implemented. Advice for those would be helpful as well! I recently decided to stop using
using namespace std; as i've read there are many dangers with this practice and frankly...writing std:: isn't all that difficult. If someone has a counterargument to this I would love to hear it as I am a beginner and could be very wrong.Main
//implementation of TicTacToe
//Using classes this time
#include
#include "TicTacToeClass.h"
int main()
{
//Assumes no play unless user decides they want to play and initializes game variable to TicTacToe class
bool play = false;
TicTacToe game;
play = game.getUserWantToPlay();
//allows for multiple games to be played
while(play == true)
{
char playerWinner = 'n';
char squareArray[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
char player = 'X';
//single game iteration
while(playerWinner == 'n')
{
game.drawBoard(squareArray);
game.getPlayerMove(squareArray, player);
playerWinner = game.checkForWin(squareArray, player);
if(playerWinner == 'n')
{
player = game.togglePlayer(player);
}
}
game.drawBoard(squareArray);
play = game.getUserWantToPlay();
}
return(0);
}Class Implementation
```
//TicTacToe class implementation
//Leeroy Jenkins
#include "TicTacToeClass.h"
#include
bool TicTacToe::getUserWantToPlay()
{
char response;
bool invalidResponse = true;
bool play = fal
Solution
Some initial thoughts...
The board
It seems a little odd that the board array is being passed into the
Checking for tie
When you're checking for a tie, you're checking for upper and lower case players ('X' and 'x'). If it wasn't for the fact that the array is being supplied you'd be guaranteed that 'x' wouldn't occur, since you only insert 'X's into the grid. You do the same 'X' or 'x' check when allowing the player to make a move.
Winning condition
When you're checking for a win, you're trusting the caller to pass in the correct player ('X' or 'O'). If the last player to take a move was 'X' and they placed a winning move, but checkForWin was called with player 'O', it would declare 'O' the winner, even though there is actually a line of 'X'.
Checking for line wins (horizontal + vertical) is easily modelled with loops, which will help to simplify your checkForWin method. I'd also suggest taking the winning player from the board, rather than from the supplied parameter.
Prompting
You convert 'X' to 1 before prompting the player to make a move. Why not just have it be X's move?
Variable Naming
In your playerMove method, you're using 'x' as an iterator variable:
Given that 'x' actually has some meaning in your game, I'd consider using a different name, even if it was just 'i'.
The board
It seems a little odd that the board array is being passed into the
TicTacToe class. I think if you really want to inject it as a dependency into the TicTacToe class, then you'd be better off wrapping it in a class. Alternately, the simpler option would be to make the board a private member variable of your class, which it then has complete control over. As it stands, both your main and your TicTacToe class have intimate knowledge about the array and it's size, although the class doesn't validate this in anyway, so if the caller creates an array that's too small things will start going wrong.Checking for tie
When you're checking for a tie, you're checking for upper and lower case players ('X' and 'x'). If it wasn't for the fact that the array is being supplied you'd be guaranteed that 'x' wouldn't occur, since you only insert 'X's into the grid. You do the same 'X' or 'x' check when allowing the player to make a move.
Winning condition
When you're checking for a win, you're trusting the caller to pass in the correct player ('X' or 'O'). If the last player to take a move was 'X' and they placed a winning move, but checkForWin was called with player 'O', it would declare 'O' the winner, even though there is actually a line of 'X'.
Checking for line wins (horizontal + vertical) is easily modelled with loops, which will help to simplify your checkForWin method. I'd also suggest taking the winning player from the board, rather than from the supplied parameter.
Prompting
You convert 'X' to 1 before prompting the player to make a move. Why not just have it be X's move?
std::cout << "Player " << playerTurn << " please make a move" << std::endl;Variable Naming
In your playerMove method, you're using 'x' as an iterator variable:
for(int x = 0; x < 9; x++)Given that 'x' actually has some meaning in your game, I'd consider using a different name, even if it was just 'i'.
Code Snippets
std::cout << "Player " << playerTurn << " please make a move" << std::endl;for(int x = 0; x < 9; x++)Context
StackExchange Code Review Q#132534, answer score: 2
Revisions (0)
No revisions yet.