0% found this document useful (0 votes)
161 views8 pages

Jerry Tac Toe

This document defines classes for a graphical Java program that implements a Tic-Tac-Toe game. It includes classes for circles and lines that make up the game board, as well as a fill class that initializes the board and checks for wins/draws. The main jerry class handles user input and game logic by allowing players to select circles and checking the results after each turn.

Uploaded by

api-638550362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views8 pages

Jerry Tac Toe

This document defines classes for a graphical Java program that implements a Tic-Tac-Toe game. It includes classes for circles and lines that make up the game board, as well as a fill class that initializes the board and checks for wins/draws. The main jerry class handles user input and game logic by allowing players to select circles and checking the results after each turn.

Uploaded by

api-638550362
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

import java.io.

*;
import java.lang.reflect.Array;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Random;
import java.awt.Color;

/*
GUI
*/

class close extends WindowAdapter {


public void windowClosing(WindowEvent e) {
System.out.println("Thank you for playing!");
System.exit(0); //stops the program
}
}

class jerry extends JFrame implements ActionListener {


class circles {
int x,y;
int size;
Color color;

public circles(int xin, int yin, int sizein, Color colorin) {


x=xin;
y=yin;
size=sizein;
color=colorin;
}

public void one() {


color=new Color(255,0,0);
}

public void two() {


color=new Color(0,0,255);
}

public circles(int xin, int yin, int sizein) {


this(xin, yin, sizein, Color.white);
}

public void draw(Graphics g) {


g.setColor(color);
g.fillOval(x/2,y/2, size/2,size/2);
g.setColor(Color.black);
g.drawOval(x/2,y/2, size/2,size/2);
}
}

class lines {
int x1, x2;
int y1, y2;
Color color;

public lines(int x1in, int y1in, int x2in, int y2in, Color colorin) {
x1=x1in;
y1=y1in;
x2=x2in;
y2=y2in;
color=colorin;

public void draw(Graphics g) {


g.setColor(color);
g.drawLine(x1/2, y1/2, x2/2, y2/2);
}
}

class fill extends JPanel {


circles [] array;
lines [] arrayTwo;

public fill() {
//circle locations
array=new circles[10];
array[0]=new circles(0,0,0, Color.lightGray);
array[1]=new circles(120, 55, 150, Color.white); //top left
array[2]=new circles(600, 55, 150, Color.white); //top middle
array[3]=new circles(1080, 55, 150, Color.white); //top right
array[4]=new circles(300, 330, 150, Color.white); //middle left
array[5]=new circles(600, 330, 150, Color.white); //middle middle
array[6]=new circles(900, 330, 150, Color.white); //middle right
array[7]=new circles(120, 605, 150, Color.white); //bottom left
array[8]=new circles(600, 605, 150, Color.white); //bottom middle
array[9]=new circles(1080, 605, 150, Color.white); //bottom right

//line locations
arrayTwo=new lines[18];
arrayTwo[0]=new lines(195, 130, 675, 130, Color.black); //line between tl
and tm
arrayTwo[1]=new lines(675, 130, 1155, 130, Color.black); //line between tm
and tr
arrayTwo[2]=new lines(195, 130, 375, 405, Color.black); //line between tl
and ml
arrayTwo[3]=new lines(675, 130, 375, 405, Color.black); //line between tm
and ml
arrayTwo[4]=new lines(675, 130, 975, 405, Color.black); //line between tm
and mr
arrayTwo[5]=new lines(1155, 130, 975, 405, Color.black); //line between tr
and mr
arrayTwo[6]=new lines(195, 130, 675, 405, Color.black); //line between tl
and mm
arrayTwo[7]=new lines(1155, 130, 675, 405, Color.black); //line between tr
and mm
arrayTwo[8]=new lines(375, 405, 675, 405, Color.black); //line between ml
and mm
arrayTwo[9]=new lines(675, 405, 975, 405, Color.black); //line between mm
and mr
arrayTwo[10]=new lines(375, 405, 195, 680, Color.black); //line between ml
and bl
arrayTwo[11]=new lines(195, 680, 675, 680, Color.black); //line between bl
and bm
arrayTwo[12]=new lines(375, 405, 675, 680, Color.black); //line between ml
and bm
arrayTwo[13]=new lines(195, 680, 675, 405, Color.black); //line between bl
and mm
arrayTwo[14]=new lines(675, 680, 1155, 680, Color.black); //line between bm
and br
arrayTwo[15]=new lines(975, 405, 1155, 680, Color.black); //line between mr
and br
arrayTwo[16]=new lines(675, 405, 1155, 680, Color.black); //line between mm
and br
arrayTwo[17]=new lines(675, 680, 975, 405, Color.black); //line between bm
and mr
}

public void one(int i) {


array[i].one();
}

public void two(int i) {


array[i].two();
}

public void resetCircles() {


for(circles c:array) {
c.color=Color.white;
}
repaint();
}

public void makeMove() {


Random random=new Random();
int index=random.nextInt(array.length);
array[index].color=Color.blue;
}

public void finish() {


if(array[1].color.equals(Color.red) && array[2].color.equals(Color.red) &&
array[3].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[1].color.equals(Color.red) &&
array[5].color.equals(Color.red) && array[9].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[1].color.equals(Color.red) &&
array[4].color.equals(Color.red) && array[8].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[2].color.equals(Color.red) &&
array[4].color.equals(Color.red) && array[7].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[2].color.equals(Color.red) &&
array[6].color.equals(Color.red) && array[9].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[3].color.equals(Color.red) &&
array[6].color.equals(Color.red) && array[8].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[3].color.equals(Color.red) &&
array[5].color.equals(Color.red) && array[7].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[4].color.equals(Color.red) &&
array[5].color.equals(Color.red) && array[6].color.equals(Color.red)) {
move.setText("User Wins!");
} else if(array[7].color.equals(Color.red) &&
array[8].color.equals(Color.red) && array[9].color.equals(Color.red)) {
move.setText("User Wins!");

} else if(array[1].color.equals(Color.blue) &&


array[2].color.equals(Color.blue) && array[3].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[1].color.equals(Color.blue) &&
array[5].color.equals(Color.blue) && array[9].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[1].color.equals(Color.blue) &&
array[4].color.equals(Color.blue) && array[8].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[2].color.equals(Color.blue) &&
array[4].color.equals(Color.blue) && array[7].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[2].color.equals(Color.blue) &&
array[6].color.equals(Color.blue) && array[9].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[3].color.equals(Color.blue) &&
array[6].color.equals(Color.blue) && array[8].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[3].color.equals(Color.blue) &&
array[5].color.equals(Color.blue) && array[7].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[4].color.equals(Color.blue) &&
array[5].color.equals(Color.blue) && array[6].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if(array[7].color.equals(Color.blue) &&
array[8].color.equals(Color.blue) && array[9].color.equals(Color.blue)) {
move.setText("Computer Wins!");
} else if((array[1].color.equals(Color.blue) ||
array[2].color.equals(Color.red)) && (array[2].color.equals(Color.blue) ||
array[2].color.equals(Color.red)) && (array[3].color.equals(Color.blue) ||
array[3].color.equals(Color.red)) && (array[4].color.equals(Color.blue) ||
array[4].color.equals(Color.red)) && (array[5].color.equals(Color.blue) ||
array[5].color.equals(Color.red)) && (array[6].color.equals(Color.blue) ||
array[6].color.equals(Color.red)) && (array[7].color.equals(Color.blue) ||
array[7].color.equals(Color.red)) && (array[8].color.equals(Color.blue) ||
array[8].color.equals(Color.red)) && (array[9].color.equals(Color.blue) ||
array[9].color.equals(Color.red))) {
move.setText("It's a draw!");

}
}

public void paintComponent(Graphics g) {


setSize(700,400);
//background
g.setColor(Color.lightGray);
g.fillRect(0,0, 700,500);

//draw lines
for(int i=0; i<arrayTwo.length; i++) {
arrayTwo[i].draw(g);
}

//draw circles
for(int i=0; i<array.length; i++) {
array[i].draw(g);
}

//add numbers to circles


g.setFont(new Font("Arial", Font.BOLD, 20));
g.setColor(Color.black);
g.drawString("1", 92,72);
g.drawString("2", 332, 72);
g.drawString("3", 572, 72);
g.drawString("4", 182, 210);
g.drawString("5", 332, 210);
g.drawString("6", 482, 210);
g.drawString("7", 92, 347);
g.drawString("8", 332, 347);
g.drawString("9", 572, 347);

}
}

fill paint;
JButton one, playAgain, first, second;
JTextField move;

public void actionPerformed(ActionEvent e) {


if(e.getSource()==one) {
int i=Integer.parseInt(move.getText());
paint.one(i);
paint.repaint();
paint.finish();
}

if(e.getSource()==playAgain) {
paint.resetCircles();
move.setText("Enter number here. Select appropriate button to make move.");
}

if(e.getSource()==second) {
paint.repaint();
paint.makeMove();
paint.finish();
}
}

public jerry() {
setTitle("Jerry-Tac-Toe");
addWindowListener(new close());
setSize(700,500);

//get the content pane


Container C=getContentPane();

//set layout manager


C.setLayout(new BorderLayout());

JPanel control=new JPanel();


control.setLayout(new BorderLayout());
C.add(control, "South");

JPanel controlTwo=new JPanel();


control.setLayout(new BorderLayout());
C.add(controlTwo, "North");

move=new JTextField("Enter number here. Select appropriate button to make


move.");
one=new JButton("Make Move");
one.addActionListener(this);
second=new JButton("Computer Move");
second.addActionListener(this);
playAgain=new JButton("Play Again");
playAgain.addActionListener(this);

control.add(move, "Center");
control.add(one, "West");
control.add(second, "East");
controlTwo.add(playAgain, "East");

//add the circles to panel


paint=new fill();
C.add(paint, "Center");

setVisible(true);
}

public static void main(String [] args) {


jerry game=new jerry();
}
}

You might also like