TicTacToe - Sample Java Coding

TicTacToe.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class TicTacToe extends Applet implements Runnable
{
 public boolean m_HumeanIsX = true;
 public ANNPlayer m_ANNPlayer;
 private HumenPlayer m_Hplayer;
 private Panel BoardPanel;
 private Button[][] Board;
 private int[][] Table;
 private GameButtons GButtons; 
 private boolean MakeMove;
 private Panel infoPanel;
 public boolean isPlayRun = false;
 private Thread playThread = null;

 public void init()
 { 
  setSize(400,400); 
  setBounds(0,0,400,400);
  setVisible(true); 
  playThread = new Thread(this);
  playThread.start(); 
 } 

 public void run() {
  while(true)
  {
   if (isPlayRun)
   {
    PlayGame();
    GButtons.Play.setEnabled(true);
    GButtons.Train.setEnabled(true);
   }
   else
   {
    if (playThread== null)
    {
     return;
    }
    try
    {
     playThread.sleep(100);
    } 
    catch (Exception e){}
   }
  }
 }
 public TicTacToe()
 {
  m_ANNPlayer = new ANNPlayer(0.1, 1.0);
  m_Hplayer     = new HumenPlayer(this);
  Board       = new Button[3][3]; 
  GButtons    = new GameButtons(this); 
  MakeMove    = false;
  Table       = new int[3][3]; 
  BoardPanel = InitBoard(this); 

  Label  Title; 
  Title=new Label("Tic Tac Toe",1);
  Title.setForeground(Color.black); 
  Font font=new Font("Times New Roman",1,18); 
  Title.setFont(font); 

  setLayout(new BorderLayout(10,10));
  setBackground(Color.white);
  infoPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
  infoPanel.add(BoardPanel);
  add(GButtons,BorderLayout.SOUTH); 
  add(infoPanel,BorderLayout.CENTER); 
  add(Title,BorderLayout.NORTH); 
 }

 private boolean tie(int[][] table)
 {
  for (int i=0;i<3;i++)
  {
   for (int j=0;j<3;j++)
   {
    if (table[i][j]==0)
    {
     return false;
    }
   }
  }
  return true;
 }

 public void PlayGame()
 {
  if (m_HumeanIsX)
  {
   MakeMove   = false;
  }
  else
  {
   MakeMove   = true;
  }
  Table = new int[3][3];
  UpdateBoard();
  int ply = 1;
  boolean firstMove = true;
  if (m_HumeanIsX)
  {
   firstMove = false;
  }
  boolean compWin = false;
  boolean humeWin = false;
  while(true)
  { 
   if(MakeMove){
    if (!firstMove)
    {
     m_ANNPlayer.markMove(Table,1);
    }
    firstMove = false;
    m_ANNPlayer.getNextMove(Table); 
    m_ANNPlayer.markMove(Table, -1);
    MakeMove=false;
    UpdateBoard();
   }
   else
   {
    try
    {
     Thread.currentThread().sleep(100);
    } 
    catch (Exception e){}
   }
   if(Win(Table,-1)){ 
    new MessageBox("Computer wins","Game Over");
    compWin = true;
    break;
   } 
   if(Win(Table,1))
   {
    m_ANNPlayer.markMove(Table,1);
    new MessageBox("You win","Game Over"); 
    humeWin = true;
    break;
   }
   if (tie(Table))
   {
    new MessageBox("Tie","Game Over");
    break;
   } 
  }
  m_ANNPlayer.finishMarkMoves(Table);
  isPlayRun = false;
 }

 public boolean Win(int[][] Table,int kind)
 {
  int[] S=new int[3];
  int i;
  int j;
  int Kind=kind;
  for (i=0;i<3;i++)
  {
   for (j=0;j<3;j++)
   {
    S[j]=Table[i][j];
   }
   if ((S[0]==S[1]) && (S[1]==S[2]) && (S[0]!=0) && S[0]==kind)
   {
    return true;
   }
  }
  for (i=0;i<3;i++)
  {
   for (j=0;j<3;j++)
   {
    S[j]=Table[j][i];
   }
   if ((S[0]==S[1]) && (S[1]==S[2]) && (S[0]!=0) && S[0]==kind)
   {
    return true;
   }
  }

  for (i=0;i<3;i++)
  {
   S[i]=Table[i][i]; 
  }
  if ((S[0]==S[1]) && (S[1]==S[2]) && (S[0]!=0) && S[0]==kind)
  {
   return true;
  }
  for (i=0;i<3;i++)
  {
   S[i]=Table[i][2-i]; 
  }
  if ((S[0]==S[1]) && (S[1]==S[2]) && (S[0]!=0) && S[0]==kind)
  {
   return true;
  }
  return false;
 }

 private void HGameMove(int x, int y)
 {
  m_Hplayer.GameMove(Table,x,y);
  MakeMove = true;

 }
 private Panel InitBoard(TicTacToe game)
 {
  Panel panel=new Panel();
  panel.setLayout(new GridLayout(3,3,10,10));
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    Board[i][j] = new Button("");
    Board[i][j].setBackground(Color.black);
    Board[i][j].setForeground(Color.white);
    Font font=new Font("Ariel",1,16);
          Board[i][j].setFont(font);
    Board[i][j].setEnabled(false);
    Board[i][j].addActionListener( 
      new ActionHandler(i,j)
      {
       public void actionPerformed(ActionEvent e)
       {
        if (isPlayRun)
        {
         HGameMove(x,y); 
        }
       }
      }
    );
    panel.add(Board[i][j]);
   }
  }
  return panel;
 }

 public void UpdateBoard()
 { 
  for(int i=0;i<3;i++){
   for(int j=0;j<3;j++){
    if(Table[i][j]==1)
    {
     if (m_HumeanIsX)
     {
      Board[i][j].setLabel("X"); 
     }
     else
     {
      Board[i][j].setLabel("O");
     }
     Board[i][j].setEnabled(false);
    } 
    else if(Table[i][j]==-1)
    {
     if (!m_HumeanIsX)
     {
      Board[i][j].setLabel("X"); 
     }
     else
     {
      Board[i][j].setLabel("O");
     }
     Board[i][j].setEnabled(false);
    }
    else
    {
     Board[i][j].setLabel("");
     Board[i][j].setEnabled(true);
    }
   }
  }
  paint(this.getGraphics());
 } 

 class ActionHandler implements ActionListener
 {
  int x,y;
  Button ButtonRef;

  ActionHandler(int num1,int num2) 
  { 
   x=num1;
   y=num2;
  }
  public void actionPerformed(ActionEvent e){} 
 }

 class GameButtons extends Panel 
    {
  TicTacToe m_game; 

  public Button Train;
  Button Play;
  TextField GameNumT;
  TextField AlphaT;
  TextField LambdaT;
  Label GameNumL;
  Label AlphaL;
  Label LambdaL;
  CheckboxGroup cbgPlayerType;
  Checkbox XPlayerType;
  Checkbox OPlayerType;
 

  public GameButtons(TicTacToe game)
  {
   m_game = game;
   setLayout(new GridLayout(0,4,10,10));

   Train=new Button("Train");
   Play=new Button("Play");
   GameNumT=new TextField("10000", 5);
   AlphaT=new TextField("0.1", 5);
   LambdaT=new TextField("1.0", 5);
   GameNumL = new Label("Games number");
   AlphaL = new Label("Alpha");
   LambdaL = new Label("Lambda");

   cbgPlayerType = new CheckboxGroup();
   XPlayerType = new Checkbox("X",cbgPlayerType,false);
   OPlayerType = new Checkbox("O",cbgPlayerType,true);

   Train.setForeground(Color.black);
   Play.setForeground(Color.black);
   GameNumT.setForeground(Color.black);
   AlphaT.setForeground(Color.black);
   LambdaT.setForeground(Color.black);
   GameNumL.setForeground(Color.black);
   AlphaL.setForeground(Color.black);
   LambdaL.setForeground(Color.black);
   XPlayerType.setForeground(Color.black);
   OPlayerType.setForeground(Color.black);

   Train.setBackground(Color.white);
   Play.setBackground(Color.white);
   GameNumT.setBackground(Color.white);
   AlphaT.setBackground(Color.white);
   LambdaT.setBackground(Color.white);
   GameNumL.setBackground(Color.white);
   AlphaL.setBackground(Color.white);
   LambdaL.setBackground(Color.white);
   XPlayerType.setBackground(Color.white);
   OPlayerType.setBackground(Color.white);
 

   Train.addActionListener(
    new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
     { 
      Play.setEnabled(false);
      Train.setEnabled(false);
      m_game.m_ANNPlayer = new ANNPlayer(Double.valueOf(AlphaT.getText()).doubleValue(), Double.valueOf(LambdaT.getText()).doubleValue());
      m_game.m_ANNPlayer.setMaxGameNum(Integer.parseInt(GameNumT.getText()));
      m_game.m_ANNPlayer.setGameNum(0);
      int player = -1;
      if (XPlayerType.getState())
      {
       player = 1;
      }
      while (m_game.m_ANNPlayer.getGameNum()<m_game.m_ANNPlayer.getMaxGameNum())
      { 
       m_game.m_ANNPlayer.trainNet(player);
       GameNumT.setText(Integer.toString(m_game.m_ANNPlayer.getGameNum() ) );
      } 
      Play.setEnabled(true);
      Train.setEnabled(true);
     }
    } 
   );
 

   Play.addActionListener(
    new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
     { 
      m_HumeanIsX = XPlayerType.getState();
      m_game.isPlayRun=true;
      Train.setEnabled(false);
      Play.setEnabled(false);
     }
    } 
   );
 

   add(AlphaL); 
   add(AlphaT);
   add(LambdaT);
   add(LambdaL); 

   add(GameNumL);
   add(GameNumT);
   add(Train);
   add(new Canvas());

   add(new Canvas());
   add(Play);
   add(XPlayerType);
   add(OPlayerType);
     } 
 } 
}

TicTacToe.java
ANNPlayer.java
NueralNet
HumenPlayer.java
MessageBox.java
TurnDialog.java
Applet1.java

Java Programming

See also
Logic For Graphs using JFreeChart

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.