[JAVA] Tic Tac Toe 02-17-2013, 09:00 PM
#1
I just made this because I was feeling bored today. It behaves strangely once in a while whose reason I'm still trying to figure out. Other than that, it works alright I guess. You have to play Tic Tac Toe against AI. It is not difficult to win, but don't take this AI lightly 
Cheers

Code:
/**
* Made By : Solixious Klein
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class TicTacToe extends Panel implements MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
byte[][] b=new byte[3][3];
JFrame fr;
int ctr=0;
JLabel info;
int win=0,loss=0,draw=0;
public TicTacToe()
{
fr=new JFrame("Tic Tac Toe");
fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
info=new JLabel("Win=0 Loss=0 Draw=0");
resetBoard(b);
fr.add(this);
fr.add(info,BorderLayout.SOUTH);
this.setBackground(new Color(255,255,255));
this.addMouseListener(this);
fr.setSize(320,355);
fr.setVisible(true);
repaint();
makeMove(b);
repaint();
}
public boolean isFull(byte[][] board)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
return false;
}
}
return true;
}
public char checkVictory(byte[][] board)
{
char victor=' ';
int v=0;
if(board[0][0]==board[0][1] && board[0][1]==board[0][2])
v=board[0][0];
else if(board[1][0]==board[1][1] && board[1][1]==board[1][2])
v=board[1][0];
else if(board[2][0]==board[2][1] && board[2][1]==board[2][2])
v=board[2][0];
else if(board[0][0]==board[1][1] && board[1][1]==board[2][2])
v=board[0][0];
else if(board[0][0]==board[1][0] && board[1][0]==board[2][0])
v=board[0][0];
else if(board[0][1]==board[1][1] && board[1][1]==board[2][1])
v=board[0][1];
else if(board[0][2]==board[1][2] && board[1][2]==board[2][2])
v=board[0][2];
else if(board[2][0]==board[1][1] && board[1][1]==board[0][2])
v=board[2][0];
else
v=0;
if(v==1)
victor='x';
else if(v==2)
victor='0';
else
victor=' ';
return victor;
}
public void makeMove(byte[][] board)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
{
board[i][j]=2;
if(checkVictory(board)=='0')
{
return;
}
else
{
board[i][j]=0;
continue;
}
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
{
board[i][j]=1;
if(checkVictory(board)=='x')
{
board[i][j]=2;
return;
}
else
{
board[i][j]=0;
continue;
}
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
{
board[i][j]=2;
if(checkTrap(board,'0'))
{
return;
}
else
{
board[i][j]=0;
continue;
}
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
{
board[i][j]=1;
if(checkTrap(board,'x'))
{
board[i][j]=2;
return;
}
else
{
board[i][j]=0;
continue;
}
}
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
{
if(i+j==2)
{
int r=(int)(Math.random()*10);
if(r%2==0)
{
board[i][j]=2;
return;
}
}
else
{
int r=(int)(Math.random()*10);
if(r<3)
{
board[i][j]=2;
return;
}
}
}
}
if(i==2)
i=0;
}
}
public boolean checkTrap(byte[][] board,char c)
{
int flag=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
{
board[i][j]=2;
if(checkVictory(board)==c)
{
flag++;
}
board[i][j]=0;
}
}
}
return (flag>1);
}
public void printBoard(byte[][] board)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==0)
System.out.print("-");
else if(board[i][j]==1)
System.out.print("x");
else if(board[i][j]==2)
System.out.print("0");
}
System.out.println();
}
}
public void resetBoard(byte[][] board)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
board[i][j]=0;
}
}
public static void main(String args[])throws Exception
{
TicTacToe ttt=new TicTacToe();
byte b[][]={{0,0,0},{0,0,0},{0,0,0}};
ttt.b=b;
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if(checkVictory(b)!=' ' || isFull(b))
{
resetBoard(b);
if(ctr%2==1)
makeMove(b);
repaint();
info.setText("Win ="+win+" Loss = "+loss+" Draw = "+draw);
return;
}
int x=e.getX();
int y=e.getY();
int i=x/100;
int j=y/100;
if(i>2)
i=2;
if(j>2)
j=2;
if(b[i][j]==0)
{
b[i][j]=1;
}
if(checkVictory(b)!=' ')
{
win++;
repaint();
ctr++;
return;
}
else if(isFull(b))
{
draw++;
repaint();
ctr++;
return;
}
makeMove(b);
if(checkVictory(b)!=' ')
{
loss++;
repaint();
ctr++;
}
else if(isFull(b))
{
draw++;
repaint();
ctr++;
}
repaint();
}
public void mouseReleased(MouseEvent e)
{
}
public void paint(Graphics g)
{
g.setColor(new Color(0,0,0));
g.drawLine(100, 0, 100, 300);
g.drawLine(200, 0, 200, 300);
g.drawLine(0, 100, 300, 100);
g.drawLine(0, 200, 300, 200);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
g.setColor(new Color(0,0,0));
if(b[i][j]==0)
{}
else if(b[i][j]==1)
{
g.setColor(new Color(255,0,0));
g.drawLine(i*100+5, j*100+15,(i*100)+100-15,(j*100)+100-5);
g.drawLine(i*100+15, j*100+5,(i*100)+100-5,(j*100)+100-15);
g.drawLine((i*100)+100-15, (j*100)+100-5,(i*100)+100-5,(j*100)+100-15);
g.drawLine(i*100+5, j*100+15,i*100+15, j*100+5);
g.drawLine(i*100+5,(j*100)+100-15,(i*100)+100-15,j*100+5);
g.drawLine(i*100+15,(j*100)+100-5,(i*100)+100-5,j*100+15);
g.drawLine(i*100+5,(j*100)+100-15,i*100+15,(j*100)+100-5);
g.drawLine((i*100)+100-15,j*100+5,(i*100)+100-5,j*100+15);
}
else if(b[i][j]==2)
{
g.setColor(new Color(0,0,255));
g.fillOval(i*100+25, j*100+10, 50, 80);
g.setColor(new Color(255,255,255));
g.fillOval((i*100+25)+10, (j*100+10)+10, 30, 60);
}
}
}
}
}Cheers
Folow me on My YouTube Channel if you're into art.

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)
This code could be shortened a lot. I don't know much about the graphics because i'm not a Java dev. I might be getting into Android programming soon however. But for the logic, I came up with some good algorithms, that would work for any sized board if needed, and any number of needed player markers in a line.
urprised: