This site requires JavaScript, please enable it in your browser!
Greenfoot back
kaciedy
kaciedy wrote ...

2012/10/27

Tic-Tac-Toe determining Tie

kaciedy kaciedy

2012/10/27

#
I was attempting to have my game determine a tie when the nine squares were filled up and there was no winner. However now I cannot play the game anymore. Everything compiles, but it is not executing basically. Is something incomplete in my code? Or am I putting the code in the wrong place? Any help is appreciated. Thank you.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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)
    {
       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) ||
        occupied[1][0].equals(letter) && occupied[1][1].equals(letter) && occupied[1][2].equals(letter) ||
        occupied[2][0].equals(letter) && occupied[2][1].equals(letter) && occupied[2][2].equals(letter) ||
        occupied[0][0].equals(letter) && occupied[1][0].equals(letter) && occupied[2][0].equals(letter) ||
        occupied[0][1].equals(letter) && occupied[1][1].equals(letter) && occupied[2][1].equals(letter) ||
        occupied[0][2].equals(letter) && occupied[1][2].equals(letter) && occupied[2][2].equals(letter) ||
        occupied[0][0].equals(letter) && occupied[1][1].equals(letter) && occupied[2][2].equals(letter) ||
        occupied[0][2].equals(letter) && occupied[1][1].equals(letter) && occupied[2][0].equals(letter))
        //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));
        }
    }
}
 
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * Write a description of class Winner here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Winner extends World
{
    /**
     * Constructor for objects of class Winner.
     *
     */
    public Winner(String letter)
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
        GreenfootImage bg = getBackground(); //getting the background image
        bg.setColor(Color.magenta);
        String text = "TIE";
        if ("o".equals(letter))
        {
            text = "Congratulations, O is the WINNER! Press ENTER to play again."
        }
        else
        if ("x".equals(letter))
        {
            text = "Congratulations, X is the WINNER! Press ENTER to play again.";
        }
        else
        {
            text = "TIE. Press ENTER to play again.";
        }
        bg.drawString(text, 125, 200);
    
}
danpost danpost

2012/10/27

#
In line 38 of your TicTacToe world, you are making a reference to the world as if it was an specific actor. That does not, by itself, in any way determine which location the mouse was clicked in the world. And still, you are trying to apply the 'getMouseInfo()' method on the 'Greenfoot' class, not on a 'MouseInfo' object.
danpost danpost

2012/10/28

#
Finally, your 'act' method in the world class never executes. There is nothing calling the method 'act(String)'. Change the method declaration to 'public void act()' (without the String parameter). The only methods the program will run automatically every cycle are act() method without any parameters.
kaciedy kaciedy

2012/10/28

#
My teacher gave our class a lot of the code, but he said for the line 38 that the reason the world is referenced like that was because the "TicTacToe" board was clicked in order for the code to execute laying down an "x" or an "o". With what I was trying to do with the string in the act method was to see if(clicks == 9) because I have tried using that under the "public void win", which I just noticed wasn't switched into the "act method". What I am supposed to find is if the first if-statement is false then the if-statement for a tie should run. If the first if-statement is false, then shouldn't the second if statement run? Thank you so very much for you help.
danpost danpost

2012/10/28

#
The 'win' method IS tied in through the 'play' method. The reason that ties are not showing up is because you are not incrementing 'clicks' until after the call to 'play'. Switch the order of the two lines and that should fix that. You still need to figure out a way to get the coordinates of where in the world the mouse is clicking. Please review the MouseInfo class documentation. If you continue to have problems, post your world act() method with your situation.
You need to login to post a reply.