[JAVA] Tic Tac Toe - Solixious - 02-17-2013
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 
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
RE: [JAVA] Tic Tac Toe - Deque - 02-18-2013
It is really fun to play it, tried and most of the time I get a tie.
Your code looks a bit messy, though. Try to seperate gui and game logic into different classes (Separation of Concerns).
If you are interested, I might publish some papers about design patterns and code smells (I've already written some).
(02-17-2013, 09:00 PM)Solixious Wrote: It behaves strangely once in a while whose reason I'm still trying to figure out.
Try if that helps:
Code: public static void main(String args[]) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TicTacToe ttt = new TicTacToe();
byte b[][] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
ttt.b = b;
}
});
}
Even if it doesn't help for your specific problem, you should start every Swing GUI this way. Otherwise you might get problems.
Look here for explanation: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
RE: [JAVA] Tic Tac Toe - Psycho_Coder - 02-18-2013
if you can then please post somethings regarding design patterns as you just told, it is always good to learn something new.:angel:
RE: [JAVA] Tic Tac Toe - Solixious - 02-18-2013
(02-18-2013, 09:27 AM)Deque Wrote: It is really fun to play it, tried and most of the time I get a tie.
Your code looks a bit messy, though. Try to seperate gui and game logic into different classes (Separation of Concerns).
If you are interested, I might publish some papers about design patterns and code smells (I've already written some).
(02-17-2013, 09:00 PM)Solixious Wrote: It behaves strangely once in a while whose reason I'm still trying to figure out.
Try if that helps:
Code: public static void main(String args[]) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TicTacToe ttt = new TicTacToe();
byte b[][] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 } };
ttt.b = b;
}
});
}
Even if it doesn't help for your specific problem, you should start every Swing GUI this way. Otherwise you might get problems.
Look here for explanation: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
Thank you for feedback Deque. I guess it is right, my codes usually look like a mess and I need to improve on that. I could use some suggestions.
I'll try the solution you provided for the swing.
Cheers
RE: [JAVA] Tic Tac Toe - Psycho_Coder - 05-19-2013
Code: public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
If you extend your class with MouseAdapter then you don't need to write these extra empty methods.
RE: [JAVA] Tic Tac Toe - ArkPhaze - 05-20-2013
What are the empty void's for? lol. And your checkVictory() function is the cheap way of doing it. 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.
RE: [JAVA] Tic Tac Toe - Deque - 05-21-2013
(05-20-2013, 10:46 PM)ArkPhaze Wrote: What are the empty void's for? lol.
He extends the MouseListener interface, so he has to implement all methods of that interface no matter if he needs them or not.
Feurex gave the right suggestion here, which is implementing the MouseAdapter instead. But actually the MouseAdapter is also just a class with empty method implementations.
Have you ever heard that Java is verbose? It definitely is. My reason to switch to Scala--it looks like the child of Java and Haskell.
RE: [JAVA] Tic Tac Toe - ArkPhaze - 05-21-2013
Ahh, I wasn't looking at the implementation... I honestly haven't heard or read much about Java lol. It just didn't really interest me before. I could grasp it though if I wanted with some time as it's similar to C#. Some very minor things that I don't like though; C# habits that would make Java programming a real b*tch.
Code: implements MouseListener
I'm also used to coding standards where that MouseListener should really be IMouseListener if that is an interface... urprised:
Perhaps that's why I overlooked it...
RE: [JAVA] Tic Tac Toe - Deque - 05-21-2013
(05-21-2013, 09:10 AM)ArkPhaze Wrote: Ahh, I wasn't looking at the implementation... I honestly haven't heard or read much about Java lol. It just didn't really interest me before. I could grasp it though if I wanted with some time as it's similar to C#. Some very minor things that I don't like though; C# habits that would make Java programming a real b*tch.
Code: implements MouseListener
I'm also used to coding standards where that MouseListener should really be IMouseListener if that is an interface... urprised:
Perhaps that's why I overlooked it...
You are right, putting the 'I' in front of interfaces is not a standard in Java.
Nor can you use any override or virtual modifier. Every method is virtual unless you declare it as final (=sealed in C#).
C# did it right to make methods final per default.
RE: [JAVA] Tic Tac Toe - ArkPhaze - 05-21-2013
Good to know before that ends up screwing me up once I get into Android development..
|