Tic Tac Toe game : Simple Gui
Tic Tac Toe is a fun game to play . This game has many variants , but the 3x3 variant is most popular .This game is played between two players . The program here is written for applet .The game has simple 3 rows and 3 columns , so make it a 3x3 grid
Rules of the Tic Tac Toe Game
The player whose turn is first has the choice to mark X or O to one of the nine squares , Here we choose X for first player .
Second player then mark its symbol O in the remaining 8 squares
The thumb of the rule is to make the three successive squaare grids of the same sign , it does not matter whether they are horizontal , vertical or diagonal , they just need to be three consecutive squares filled with same symbol X or O.
First player who is able to make three consecutive squares with same symbol WINS .
You can find the Best gui Tic tac toe game java code along with clear and exit options
Console based Tic tac toe java code is here :This is a console based tic tac toe game
Pseudo code for Tic Tac Toe Game :
1. Board is drawn first
2. Player X and Player O click the mouse on the empty grid of the board
3. Now Check at each click on the board
3.1 Is there any three vertical grids with same Mark (i.e X or O)
if true
Player with that mark Wins and Stop
3.2 Is there any three horizontal grids with same Mark (i.e X or O)
if true
Player with that mark Wins and Stop
3.3 All grids filled
if true
Declare result is stalemate
4. Repeat step 2
Demo :
Tic Tac Toe Game Java Code
import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class TicTacToe extends Applet implements MouseListener { Frame f; int flag=2,n,m,i=0; char ch[]=new char[9]; public TicTacToe() { f=new Frame("Tic Tac Toe"); f.setLayout(null); f.setVisible(true); f.setSize(600,600); f.addMouseListener(this); for(i=0;i<9;i++) ch[i]='B'; } public void mouseClicked(MouseEvent e) { Graphics g=f.getGraphics(); g.drawLine(200,0,200,600); g.drawLine(400,0,400,600); g.drawLine(0,200,600,200); g.drawLine(0,400,600,400); flag--; int x=e.getX(); int y=e.getY(); if(flag==1) { if(x<200&&y<200){m=0;n=0;ch[0]='R';} if((x>200&&x<400)&&(y<200)){m=200;n=0;ch[1]='R';} if((x>400&&x<600)&&(y<200)){m=400;n=0;ch[2]='R';} if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='R';} if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='R';} if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='R';} if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='R';} if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='R';} if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='R';} g.setColor(Color.red); g.drawLine(m,n,m+199,n+199); g.drawLine(m+199,n,m,n+199); } if(flag==0) { if(x<200&&y<200){m=0;n=20;ch[0]='P';} if((x>200&&x<400)&&(y<200)){m=200;n=20;ch[1]='P';} if((x>400&&x<600)&&(y<200)){m=400;n=20;ch[2]='P';} if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='P';} if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='P';} if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='P';} if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='P';} if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='P';} if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='P';} g.setColor(Color.green); g.drawOval(m+10,n+10,169,169); // g.drawLine(m,n,m+189,n+189); // g.drawLine(m+199,n,m,n+199); flag=flag+2; } for(i=0;i<9;i++) // for draw { if(ch[i]!='B') { if(i==8) draw(); } else break; } for(i=0;i<3;i++) //for vertical { // System.out.print(ch[i]); if(ch[i]!='B') { if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i])) win(); } } for(i=0;i<7;i++) //for horizontal { if(ch[i]!='B') { if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2])) win(); i=i+2; } else i=i+2; } if(ch[4]!='B') //for diagonals { if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6]))) win(); } } public Frame win() { Frame m=new Frame("Result"); Label l=new Label("you win"); m.setLayout(null); m.add(l); l.setBounds(20,20,60,60); m.setVisible(true); m.setSize(100,100); return m; } public Frame draw() { Frame m=new Frame("Result"); Label l1=new Label("Stalemate"); m.setLayout(null); m.add(l1); l1.setBounds(20,20,60,60); m.setVisible(true); m.setSize(100,100); return m; } public void mouseReleased(MouseEvent e) { System.out.print(""); } public void mouseEntered(MouseEvent e) { System.out.print(""); } public void mouseExited(MouseEvent e) { System.out.print(""); } public void mousePressed(MouseEvent e) { System.out.print(""); } public static void main(String args []) { new TicTacToe(); } }
0 comments:
Post a Comment