patterncppMinor
Tic-Tac-Toe optimization
Viewed 0 times
tacoptimizationtictoe
Problem
I've made a Tic-Tac-Toe program (non-AI) for 2 human players. I will implement AI for a computer player later on. I am a beginner programmer and am also new to classes, which I've implemented in this program. I want to know how this could be optimized.
```
//the main class is all the way at the end
#include "stdafx.h"
#include
#include
#include
//To Implement Color Mechanics
HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c){
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
//As of now the program asks for player names but this functionality was added later on, but that disrupted some methods in the game class
//so i declared these global variables to keep the changes to a minimum
std::string player1, player2;
//its the game class, all the methods and the constructor are written right after the class
class game{
public:
game();
int insert(int,char);
int win();
void win_display();
private:
char board[9];
void color_conv(char);
void Winner(int);
};
//constructor for the class game
game::game(){
SetColor(DARKGREEN);
std::cout9){
SetColor(RED);
std::cout if its not a win and the board is full i.e. no '-' chars
while(true){
for(int k=0;k>choice;
turn_check=current_game.insert(choice,'O');
}
//check if the game is won
check=current_game.win();
//player 2's turn
if(i%2==0){
std::cout>choice;
turn_check=current_game.insert(choice,'X');
}
//check if the game is won
check=current_game.win();
//increment i for the next player's turn
i++;
//if player has not made a valid chance give the player another chance
if(turn_check==0){
i--;
}
}
//at this point check=1 or 2 i.e. game is won or draw the following conditional stat
```
//the main class is all the way at the end
#include "stdafx.h"
#include
#include
#include
//To Implement Color Mechanics
HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
void SetColor(Color c){
if(hCon == NULL)
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon, c);
}
//As of now the program asks for player names but this functionality was added later on, but that disrupted some methods in the game class
//so i declared these global variables to keep the changes to a minimum
std::string player1, player2;
//its the game class, all the methods and the constructor are written right after the class
class game{
public:
game();
int insert(int,char);
int win();
void win_display();
private:
char board[9];
void color_conv(char);
void Winner(int);
};
//constructor for the class game
game::game(){
SetColor(DARKGREEN);
std::cout9){
SetColor(RED);
std::cout if its not a win and the board is full i.e. no '-' chars
while(true){
for(int k=0;k>choice;
turn_check=current_game.insert(choice,'O');
}
//check if the game is won
check=current_game.win();
//player 2's turn
if(i%2==0){
std::cout>choice;
turn_check=current_game.insert(choice,'X');
}
//check if the game is won
check=current_game.win();
//increment i for the next player's turn
i++;
//if player has not made a valid chance give the player another chance
if(turn_check==0){
i--;
}
}
//at this point check=1 or 2 i.e. game is won or draw the following conditional stat
Solution
Pseudo code for a simple 'AI':
The corner and middle edges can be decided randomly so that the player has more fun via variety.
For another twist, and making the 'AI' both smarter, and dumber in ways, you could the above pseudo code so that there is a chance (random) of picking an action from a different 'tier'. The 'AI' would be smarter because it has a wider range of options, but dumber because they don't work as well.
// If there is a winning move
// Take it.
// Otherwise, if there is a blocking move (opponent has 2 in a row with open square)
// Take it.
// Otherwise, take one of these spots, if it is available, in this order:
// Middle, Middle.
// Any corner. // <-- this can be randomized for different games.
// Any middle edge. // <-- same as above.The corner and middle edges can be decided randomly so that the player has more fun via variety.
For another twist, and making the 'AI' both smarter, and dumber in ways, you could the above pseudo code so that there is a chance (random) of picking an action from a different 'tier'. The 'AI' would be smarter because it has a wider range of options, but dumber because they don't work as well.
Code Snippets
// If there is a winning move
// Take it.
// Otherwise, if there is a blocking move (opponent has 2 in a row with open square)
// Take it.
// Otherwise, take one of these spots, if it is available, in this order:
// Middle, Middle.
// Any corner. // <-- this can be randomized for different games.
// Any middle edge. // <-- same as above.Context
StackExchange Code Review Q#30060, answer score: 3
Revisions (0)
No revisions yet.