0% found this document useful (0 votes)
30 views

Import

This Java code implements a Tic-Tac-Toe game. The TicTacToe class represents the game world, tracking whose turn it is and the state of the board. Players place X's and O's by clicking on the board, and the game checks for wins or a draw after each turn. If someone wins or it's a draw, the world changes to a Winner world that displays a message based on the outcome.

Uploaded by

Tac Tac
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Import

This Java code implements a Tic-Tac-Toe game. The TicTacToe class represents the game world, tracking whose turn it is and the state of the board. Players place X's and O's by clicking on the board, and the game checks for wins or a draw after each turn. If someone wins or it's a draw, the world changes to a Winner world that displays a message based on the outcome.

Uploaded by

Tac Tac
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


import java.awt.Color;
/**
* Write a description of class TicTacToe here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TicTacToe extends World
{
private boolean turn;//true =Player1, false= Player 2
private String[][] occupied = {{"", "", ""},
{"", "", ""},
{"", "", ""}};
private int clicks;
/**
* Constructor for objects of class TicTacToe.
*
*/
public TicTacToe()
{
super(3, 3, 100);
turn = true; //Player1 gets to go first
GreenfootImage bg = getBackground(); //storing the background image to edit it
bg.setColor(Color.MAGENTA);
bg.drawLine(100, 0, 100, 300);
bg.setColor(Color.YELLOW);
bg.drawLine(200, 0, 200, 300);
bg.setColor(Color.GREEN);
bg.drawLine(0, 100, 300, 100);
bg.setColor(Color.ORANGE);
bg.drawLine(0, 200, 300, 200);
}
public void act(String letter)
{
if(Greenfoot.mouseClicked(this))
{
int x = Greenfoot.getMouseInfo().getX();
int y = Greenfoot.getMouseInfo().getY();
play(x, y);
clicks = clicks + 1;
}
}
public void play(int x, int y)
{

49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.

if(turn && occupied[y][x].equals(""))


{
addObject(new Player1(), x, y);
occupied[y][x] = "x";
win("x");
turn = false;
}
else if(!turn && occupied[y][x].equals(""))
{
addObject(new Player2(), x, y);
occupied[y][x] = "o";
win("o");
turn = true;
}
}
public void win(String letter)
{
if(occupied[0][0].equals(letter) && occupied[0][1].equals(letter) && occupied[0][2].equals(letter
) ||

68.

occupied[1][0].equals(letter) && occupied[1][1].equals(letter) && occupied[1][2].equals(letter) |


|

69.

occupied[2][0].equals(letter) && occupied[2][1].equals(letter) && occupied[2][2].equals(letter) |


|

70.

occupied[0][0].equals(letter) && occupied[1][0].equals(letter) && occupied[2][0].equals(letter) |


|

71.

occupied[0][1].equals(letter) && occupied[1][1].equals(letter) && occupied[2][1].equals(letter) |


|

72.

occupied[0][2].equals(letter) && occupied[1][2].equals(letter) && occupied[2][2].equals(letter) |


|

73.

occupied[0][0].equals(letter) && occupied[1][1].equals(letter) && occupied[2][2].equals(letter) |


|

74.

occupied[0][2].equals(letter) && occupied[1][1].equals(letter) && occupied[2][0].equals(letter))

75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86. }
87. }
88.

//if the top row has the same letters


{
Greenfoot.playSound("fanfare.wav");
Greenfoot.setWorld(new Winner(letter));
}
if(clicks == 9)
{
Greenfoot.playSound("au.wav");
Greenfoot.setWorld(new Winner(letter));
}

89.
90. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
91. import java.awt.Color;
92.
93. /**
94. * Write a description of class Winner here.
95. *
96. * @author (your name)
97. * @version (a version number or a date)
98. */
99. public class Winner extends World
100.
{
101.
/**
102.
* Constructor for objects of class Winner.
103.
*
104.
*/
105.
public Winner(String letter)
106.
{
107.
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
108.
super(600, 400, 1);
109.
110.
GreenfootImage bg = getBackground(); //getting the background image
111.
bg.setColor(Color.magenta);
112.
String text = "TIE";
113.
if ("o".equals(letter))
114.
{
115.
text = "Congratulations, O is the WINNER! Press ENTER to play again.";
116.
}
117.
else
118.
if ("x".equals(letter))
119.
{
120.
text = "Congratulations, X is the WINNER! Press ENTER to play again.";
121.
}
122.
else
123.
{
124.
text = "TIE. Press ENTER to play again.";
125.
}
126.
bg.drawString(text, 125, 200);
127.
}
128.
}

You might also like