Machine game: Tic-tac-toe game

Source: Internet
Author: User

Below is a simple tic-tac-toe game I wrote.

 

My main idea is to use extremely small search strategies. In addition, the quality of the game program depends largely on the performance of the situation evaluation function.

This is because the machine wants to select the optimal footwork in the next collection. This requires an assessment of the situation and additional knowledge, and the evaluation of the situation can be the sum of the results of all these knowledge evaluations.

 

The evaluation function and program are inversely proportional to the local search speed.

 

 

The evaluation functions in my program are relatively weak.

 

There are two main types:

 

~~~~~~~~~~~~~~~~~~~~ Map. h ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '

 

# Ifndef map_h
# Define map_h

# Include <iostream>
# Include <cstring>
# Define max_depth 2
Using namespace STD;

Class map {
Public:

Map (){


}
 
// The computer try to make best Movement
Int makebestmove (player & Computer ){

Int bestvalue = minmaxsearch (max_depth, computer. type );
Cout <"Computer (x, y):" <bestmove. x <"" <bestmove. Y <Endl;
Makemove (computer. type, bestmove. X, bestmove. y );
Return 0;
}
 
// Check wether the board is all occupied
Bool isalloccupied (){

Bool result = true;
For (INT I = 1; I <= boardsize; I ++ ){

For (Int J = 1; j <= boardsize; j ++ ){

If ('0' = Board [I] [J]) {
Result = false;
Return result;
}

}
}
Return result;
}
 
// Decide wether one side has won
Bool ishuamanorcomputerwin (const Int & side ){


Bool win = false;
Char flag = (side = 1 )? 'C': 'H ';

Int I;

For (I = 1; I <= boardsize; I ++) {// row

If (flag = Board [I] [0] & flag = Board [I] [1] & flag = Board [I] [2]) {

Win = true;
Return win;
}

}

For (I = 1; I <= boardsize; I ++) {// Column

If (flag = Board [0] [I] & flag = Board [1] [I] & flag = Board [2] [I]) {

Win = true;
Return win;
}

}

If (Board [1] [1] = Board [2] [2]) & (Board [1] [1] = Board [3] [3]) |
(Board [3] [1] = Board [2] [2]) & (Board [3] [1] = Board [1] [3])
{

Win = true;
Return win;
}

Return win;
}
 
// 1: computer-1: Human 'C': Computer 'H': Human
Bool makemove (const Int & side, const Int & X, const Int & Y ){

If (x <= 0 | x> = 4 | Y <= 0 | Y> = 4 | Board [x] [Y]! = '0 ')
Return false;

Board [x] [Y] = (1 = side )? 'C': 'H ';
Return true;
}
// Umake previous Movement
Bool unmakemove (const Int & X, const Int & Y ){

If (x <= 0 | x> = 4 | Y <= 0 | Y> = 4 | Board [x] [Y] = '0 ')
Return false;

Board [x] [Y] = '0 ';
Return true;
}
 
// Print the Board
Void printboard (){

For (INT I = 1; I <= boardsize; I ++ ){
For (Int J = 1; j <= boardsize; j ++ ){

Cout <Board [I] [J] <"";
}
Cout <Endl;
}
}
 
// Max-min search strategy
Int minmaxsearch (INT depth, int side ){

Int value, best;
Move movearray [9]; // Save the sub-location

If (1 = side)
Best = int_min;
If (-1 = side)
Best = int_max;

If (0 = depth ){

Return evaluatemap ();
}

Int num = genallmove (movearray );

For (INT I = 0; I <num; I ++ ){

Makemove (side, movearray [I]. X, movearray [I]. y );
Value = minmaxsearch (depth-1,-1 * side );
Unmakemove (movearray [I]. X, movearray [I]. y );

If (1 = side ){

If (value> Best ){

Best = value;
If (max_depth = depth)
Bestmove = movearray [I];

}
}

If (-1 = side ){

If (value <best ){

Best = value;
If (max_depth = depth)
Bestmove = movearray [I];
}
}

}

Return best; // return the optimal value of the player.

}
 
PRIVATE:

 
 
Static char Board [4] [4];
Enum {boardsize = 3 };
 
Typedef struct move {
Int X;
Int y;
} Move;
 
Move bestmove; // The best sub-location
 
Int genallmove (move * array) {// here, the move definition should be placed before the reference to move

Int movenum = 0;

For (INT I = 1; I <= boardsize; I ++ ){

For (Int J = 1; j <= boardsize; j ++ ){

If ('0' = Board [I] [J]) {

Array [movenum]. x = I;
Array [movenum]. Y = J;
Movenum ++;
}
}
}

Return movenum;
}
 
Int evaluatemap (){

Char boardcopy [4] [4];
Memcpy (boardcopy, board, sizeof (board); // back up the original Board
Int comp = 0, human = 0;

For (INT I = 1; I <= boardsize; I ++ ){

For (Int J = 1; j <= boardsize; j ++ ){

If ('c' = Board [I] [J]) {

Comp + = util (I, j, 'C ');
}

If ('H' = Board [I] [J]) {

Human + = util (I, j, 'H ');
}
}
}

Memcpy (board, boardcopy, sizeof (boardcopy); // restore the original Board

Return comp-human;

}
 

Int util (int x, int y, char c ){


Int score = 0, I, J;

For (int K = 0; k <8; k ++ ){

I = x + dir [k] [0]; // 2 links
J = Y + dir [k] [1];

If (I <= 0 | I> = 4 | j <= 0 | j> = 4 | C! = Board [I] [J])
Continue;

Score + = 4;

I = I + dir [k] [0]; // test again in the same direction
J = J + dir [k] [1];

If (I <= 0 | I> = 4 | j <= 0 | j> = 4 | C! = Board [I] [J])
Continue;

Score-= 4;
Score + = 16;

}

Board [x] [Y] = '0 ';

If (0 = score)
Return 1;
Else
Return score;

}
 

Const static int dir [8] [2];
};

# Endif

Char map: Board [4] [4] = {
 
'0', '0', '0', '0 ',
'0', '0', '0', '0 ',
'0', '0', '0', '0 ',
'0', '0', '0', '0'
};

Const int map: dir [8] [2] = {0,-1}, {-}, {0, 1}, {}, {-1, -1}, {-}, {1,-1 }};

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~ Player. h ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

# Ifndef player_h
# Define player_h

Class player {
 
Public:
Player (int t): type (t ){

}
Public:
 
Int posx; // records the current position of the player.
Int posy;
Int type;
};
# Endif

~~~~~~~~~~~~~~~~~~~~~~~ Main Program ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

# Include <iostream>
# Include "Player. H"
# Include "map. H"
# Define not!
Using namespace STD;

Player computer (1), human (-1); // 1 corresponds to computer-1 corresponds to human
Map map;

Int main (){
 
Cout <"tic-tac-toe game! "<Endl;
 
Cout <"computer make first step..." <Endl;
Computer. posx = 2;
Computer. Posy = 2;
Map. makemove (computer. type, computer. posx, computer. Posy );
Map. printboard ();
 
Bool movecorrectflag = true;
 
// Start the game
While (not map. isalloccupied ()){

Cout <"make step (x, y ):";
Cin> Human. posx> Human. Posy;
Movecorrectflag = map. makemove (human. type, human. posx, human. Posy );
If (false = movecorrectflag ){

Cout <"invalid move... try again! "<Endl;
Continue;
}

Else {

Map. printboard ();
If (Map. ishuamanorcomputerwin (human. Type) {// if human win

Cout <"you win! "<Endl;
Return 0;
}
Else {

Map. makebestmove (Computer );
Map. printboard ();
If (Map. ishuamanorcomputerwin (computer. Type )){

Cout <"You lost! "<Endl;
Return 0;
}
}
}
}
 
Cout <"a draw... the game ends! "<Endl;
 
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.