Tic-Tac-Toe游戏

 1 //用来存储一步的类
 2  final class Best {
 3
 4      int  row;
 5      int  column;
 6      int val;
 7
 8
 9      public    Best(int  v)
10      {
11          this(v,0,0);
12      }
13      public   Best(int   v,int r,int c)
14      {
15          val=v;
16          row=r;
17          column=c;
18      }
19 }
//position类
final  class Position {
	private   int    [][]board;

	public   Position  (int  [][]  theBoard)
	{
		board=new   int [3][3];
		for(int i=0;i<3;i++)
			for(int j=0;j<3;j++)
				board[i][j]=theBoard[i][j];

	}
	public  boolean  equals(Object  rhs)
	{
		if(!(rhs  instanceof  Position))
			return  false;
		//Position  other=(Position)hrs;

		for(int i=0;i<3;i++)
          for(int j=0;j<3;j++)
        	  if(board[i][j]!=((Position)rhs).board[i][j])
        		  return  false;
		return  true;
	}
public int  hashCode()
{
	int  hashVal=0;

	for(int i=0;i<3;i++)
		for(int j=0;j<3;j++)
			hashVal=hashVal*4+board[i][j];

	return  hashVal;
}
}

  

  1 import java.awt.Frame;
  2 import java.awt.Panel;
  3 import java.awt.Button;
  4 import java.awt.GridLayout;
  5
  6 import java.awt.event.WindowAdapter;
  7 import java.awt.event.ActionListener;
  8 import java.awt.event.ActionEvent;
  9 import java.awt.event.WindowEvent;
 10
 11 public class TicTacMain extends Frame
 12 {
 13     /**
 14      *
 15      */
 16     private static final long serialVersionUID = -1712977983195809884L;
 17
 18     public TicTacMain( )
 19     {
 20         add( new TicTacPanel( ) );
 21
 22         addWindowListener( new WindowAdapter( )
 23         {
 24             public void windowClosing( WindowEvent event )
 25             {
 26                 System.exit( 0 );
 27             }
 28         } );
 29     }
 30
 31
 32     public static void main( String [ ] args )
 33     {
 34         Frame f = new TicTacMain( );
 35         f.pack( );
 36         f.setVisible( true );
 37     }
 38
 39     private static class TicTacPanel extends Panel implements ActionListener
 40     {
 41         public TicTacPanel( )
 42         {
 43             setLayout( new GridLayout( 3, 3 ) );
 44             for( int i = 0; i < 3; i++ )
 45                for( int j = 0; j < 3; j++ )
 46                {
 47                    squares[ i ][ j ] = new Button( );
 48                    add( squares[ i ][ j ] );
 49                    squares[ i ][ j ].addActionListener( this );
 50                }
 51
 52
 53             resetBoard( );
 54         }
 55
 56         public void resetBoard( )
 57         {
 58             t = new TicTacToe( );
 59             for( int i = 0; i < 3; i++ )
 60                for( int j = 0; j < 3; j++ )
 61                {
 62                    squares[ i ][ j ].setLabel( "" );
 63                    squares[ i ][ j ].setEnabled( true );
 64                }
 65         }
 66
 67         private int gameNum = 0;
 68
 69         public void doCompMove( boolean thinkAboutIt )
 70         {
 71             Best compMove;
 72
 73             if( thinkAboutIt )
 74                 compMove = t.chooseMove( TicTacToe.COMPUTER );
 75             else
 76             {
 77                 compMove = new Best( 0, gameNum % 3, gameNum / 3 );
 78                 gameNum = ( gameNum + 1 ) % 9;
 79             }
 80
 81             System.out.println( " ROW = " + compMove.row +
 82                                 " COL = " + compMove.column );
 83
 84             squares[ compMove.row ][ compMove.column ].setLabel( computerSide );
 85             squares[ compMove.row ][ compMove.column ].setEnabled( false );
 86             t.playMove( TicTacToe.COMPUTER, compMove.row, compMove.column );
 87         }
 88
 89         public boolean resetIfDone( boolean condition, String message, boolean compMoves )
 90         {
 91             if( condition )
 92             {
 93                 System.out.println( message );
 94                 System.out.println( "Restarting..." );
 95                 resetBoard( );
 96                 if( compMoves )
 97                 {
 98                     System.out.println( "I go first..." );
 99                     computerSide = "X";
100                     humanSide = "O";
101                     doCompMove( false );
102                 }
103                 else
104                 {
105                     humanSide = "X";
106                     computerSide = "O";
107                     System.out.println( "You go first..." );
108                 }
109             }
110             return condition;
111         }
112
113
114         public void actionPerformed( ActionEvent evt )
115         {
116             if( evt.getSource( ) instanceof Button )
117             {
118                 ( (Button)evt.getSource( ) ).setLabel( humanSide );
119                 ( (Button)evt.getSource( ) ).setEnabled( false );
120
121                 for( int i = 0; i < 3; i++ )
122                     for( int j = 0; j < 3; j++ )
123                         if( evt.getSource( ) == squares[ i ][ j ] )
124                             t.playMove( TicTacToe.HUMAN, i, j );
125
126                 if( resetIfDone( t.boardIsFull( ), "DRAW", true ) )
127                     return;
128                 doCompMove( true );
129                 resetIfDone( t.isAWin( TicTacToe.COMPUTER ), "I WIN!!", true );
130                 resetIfDone( t.boardIsFull( ), "DRAW", false );
131
132                 return;
133             }
134         }
135
136         private Button [ ][ ]  squares = new Button[ 3 ][ 3 ];
137         private TicTacToe t;
138         private String computerSide = "O";
139         private String humanSide    = "X";
140     }
141 }
  1 package s2;
  2
  3 //结合了置换表和剪枝的TicTacToe类
  4 import  java.util.Map;
  5 import  java.util.HashMap;
  6 //TicTacToe类的框架
  7  class TicTacToe {
  8      public  static  final  int   HUMAN=0;
  9      public  static  final  int  COMPUTER=1;
 10      public  static   final  int   EMPTY=2;
 11
 12      public  static  final  int   HUMAN_WIN=0;
 13      public  static   final   int     DRAW=1;
 14      public  static  final   int     UNCLEAR=2;
 15      public  static   final   int    COMPUTER_WIN=3;
 16
 17      public     TicTacToe()
 18      {
 19             clearBoard();
 20
 21      }
 22
 23
 24     private   Map<Position,Integer>transpositions=new   HashMap<Position,Integer>();
 25     public  Best    chooseMove(int  size)
 26     {
 27         return  chooseMove(size,HUMAN_WIN,COMPUTER_WIN,0);
 28     }
 29            //Find  optional  move
 30     private  Best  chooseMove(int side,int  alpha,int beta,int  depth)
 31     {
 32         int  opp;
 33         Best   reply;
 34
 35         int simpleEval;
 36         Position   thisPosition=new  Position(board);
 37         int  tableDepth=5;
 38         int  bestRow=0;
 39         int bestColumn=0;
 40         int value;
 41
 42
 43
 44
 45         if((simpleEval=positionValue())!=UNCLEAR)
 46             return  new   Best(simpleEval);
 47         if(depth==0)
 48             transpositions.clear();
 49         else if(depth>3&&depth<=tableDepth)
 50         {
 51             Integer  lookupVal=transpositions.get(thisPosition);
 52             if(lookupVal!=null)
 53                 return  new   Best(lookupVal);
 54         }
 55         if(side==COMPUTER)
 56         {
 57             opp=HUMAN;
 58             value=alpha;
 59         }
 60         else
 61         {
 62             opp=COMPUTER;
 63             value=beta;
 64         }
 65
 66         Outer:
 67             for(int row =0;row<3;row++)
 68                 for(int  column=0;column<3;column++)
 69                     if(squareIsEmpty(row,column))
 70                     {
 71                         place(row,column,side);
 72                         reply=chooseMove(opp,alpha,beta,depth+1);
 73                         place(row,column,EMPTY);
 74                         if(side==COMPUTER&&reply.val>value||side==HUMAN&&reply.val<value)
 75                         {
 76                             if(side==COMPUTER)
 77                                 alpha=value=reply.val;
 78                             else
 79                                 beta=value=reply.val;
 80
 81
 82                             bestRow=row;
 83                             bestColumn=column;
 84                             if(alpha>=beta)
 85                                 break  Outer;    //Refution
 86                         }
 87                     }
 88         if(depth<=tableDepth)
 89             transpositions.put(thisPosition,value);
 90              return   new   Best(value,bestRow,bestColumn);
 91
 92
 93     }
 94     private   int    positionValue()   /*估算位置的程序,根据棋盘上的情况返回HUMAN_WIN\DRAW\COMPUTER_WIN\UNCLEAR*
 95     所使用的策略是最大最小值策略*/
 96     {
 97         return   isAWin(COMPUTER)?COMPUTER_WIN:
 98                  isAWin(HUMAN)    ?HUMAN_WIN:
 99                  boardIsFull()   ?DRAW   :UNCLEAR;
100
101     }
102     public   boolean     playMove(int row,int column,int side)
103     {
104         if( row < 0 || row >= 3 || column < 0 || column >= 3||board[row][column]!=EMPTY)
105             return  false;
106         board[ row ][ column ] = side;
107         return true;
108     }
109     public   void   clearBoard()
110     {
111           for( int i = 0; i < 3; i++ )
112                 for( int j = 0;j<3;j++)
113                     board[i][j]=EMPTY;
114     }
115     public  boolean  boardIsFull()
116     {
117          for( int row = 0; row < 3; row++ )
118                 for( int column = 0; column < 3; column++ )
119                     if( board[ row ][ column ] == EMPTY )
120                         return false;
121             return true;
122     }
123     public   boolean  isAWin(int  side)
124     {
125          int row, column;
126
127          /* Look for all in a row */
128      for( row = 0; row < 3; row++ )
129      {
130          for( column = 0; column < 3; column++ )
131              if( board[ row ][ column ] != side )
132                  break;
133          if( column >= 3 )
134              return true;
135      }
136
137      /* Look for all in a column */
138      for( column = 0; column < 3; column++ )
139      {
140          for( row = 0; row < 3; row++ )
141              if( board[ row ][ column ] != side )
142                  break;
143          if( row >= 3 )
144              return true;
145      }
146
147      /* Look on diagonals */
148      if( board[ 1 ][ 1 ] == side && board[ 2 ][ 2 ] == side
149             && board[ 0 ][ 0 ] == side )
150          return true;
151
152      if( board[ 0 ][ 2 ] == side && board[ 1 ][ 1 ] == side
153             && board[ 2 ][ 0 ] == side )
154          return true;
155
156      return false;
157     }
158     private     void    place(int  row,int  column,int  piece)
159     {
160         board[row][column]=piece;
161         //Test   if   a  square   is  Empty
162     }
163     public   boolean  squareIsEmpty(int  row,int  column)
164     {
165         return  board[row][column]==EMPTY;
166
167     }
168         private  int  [][]board=new  int[3][3];    //创建一个网格
169 }
时间: 2024-10-13 08:53:17

Tic-Tac-Toe游戏的相关文章

amazon.设计1. tic tac toe

//不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html 类似相关棋牌坐标对战游戏 一通百通 /** * Enumerations for the various states of the game */ public enum GameState { // to save as "G

2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

题面: C. Team Tic Tac Toe Input ?le: standard input Output ?le: standard output Time limit: 1 second Memory limit: 256 megabytes Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer J

tic tac toe

井字棋 tic tac toe,布布扣,bubuko.com

Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputing-001/wiki/view? page=trees 1.2 CODE无parent域的树 http://www.codeskulptor.org/#poc_tree.py class Tree: """ Recursive definition for trees plus

【leetcode】1275. Find Winner on a Tic Tac Toe Game

题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-Toe: Players take turns placing characters into empty squares (" "). The first player A always places "X" characters, while the second pl

[CareerCup] 17.2 Tic Tac Toe 井字棋游戏

17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏,有下面几点需要考虑: 1. 判断是否能赢hasWon函数是调用一次还是多次,如果是多次,我们可能为了优化而需要加入一些预处理. 2. 井字棋游戏通常是3x3的大小,我们是否想要实现NxN的大小? 3. 我们需要在代码紧凑,执行速度和代码清晰之间做出选择. #include <iostream> #

LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: 玩家轮流将棋子放在空方格 (” “) 上.第一个玩家 A 总是用 “X” 作为棋子,而第二个玩家 B 总是用 “O” 作为棋子.“X” 和 “O” 只能放在空方格中,而不能放在已经被占用的方格上.只要有 3 个相同的(非空)棋子排成一条直线(行.列.对角线)时,游戏结束.如果所有方块都放满棋子(不为

Epic - Tic Tac Toe

N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If 3 consecutive samecolor found, that color will get 1 point. So if 4 red are vertically then pointis 2. Find the winner. def tic_tac_toe(board,n) red,

Learn Python 015: Tic Tac Toe Game

board = [' ' for i in range(9)] def print_board(): row_1 = '|{}|{}|{}|'.format(board[0], board[1], board[2]) row_2 = '|{}|{}|{}|'.format(board[3], board[4], board[5]) row_3 = '|{}|{}|{}|'.format(board[6], board[7], board[8]) print('\n') print(row_1)

UVa 10363 - Tic Tac Toe

题目:给你一个井字棋的状态,判断是否合法. 分析:枚举.直接枚举多有情况判断即可. 合法状态有三种情况:(X先下子) 1.X赢,则O不能赢,且X比O多一子: 2.O赢,则X不能赢,且O和X子一样多: 3.没人赢,此时O的子和可能和X一样多,也可能少一个. 说明:简单题(⊙_⊙). #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include &l