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

2012/10/24

Tic Tac Toe Finding Winner

kaciedy kaciedy

2012/10/24

#
I know I need to use an if statement in order to determine if "x" or "o" is the winner or if there is a tie. At one point I was able to get the screen that shows up to say TIE, but it did it even if x or o went. I really have no idea what could improve the code. Any help is greatly appreciated. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color;
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
{
    private int clicks;
    
    /**
     * 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);
        if(Greenfoot.mouseClicked("o"))
        {   
            bg.drawString("Congratulations, O is the WINNER!", 300, 200);
        }
        else
        if(Greenfoot.mouseClicked("x"))
        {
            bg.drawString("Congratulations, X is the WINNER!", 300, 200);
        }
        else
        if(clicks >= 9)
        {
            bg.drawString("TIE", 300, 200);
        }
    }
danpost danpost

2012/10/24

#
The decision-making to see if there is a winner or tie should be done in your game-playing world (not in the world that displays who won or not). You do, however, have a String being received by the constructor from the previous world which would be best used to transmit the text that needs displayed. I do not see where the variable 'clicks' is used; though, I can tell you this -- when line 36 executes the value of 'clicks' will be zero (0), to be sure (therefore, the 'TIE' message will never show). But, if you move the decision-making to the game-playing world where the variable 'clicks' is (should be) located, then that will no longer be an issue (in this world).
kaciedy kaciedy

2012/10/24

#
In my Tic-Tac-Toe world class it determines if there is a winner or not, however not a tie. I took out "clicks" completely from the Winner world class, and now it resumes the tie screen for any type of win. Thank you!
danpost danpost

2012/10/24

#
In the Winner world constructor, you probably do not want to check for mouseClicks. Just check the 'letter' that is brought in
String text = "TIE";
if ("o".equals(letter)) text = "Congratulations, O is the WINNER!";
if ("x".equals(letter)) text = "Congratulations, X is the WINNER!";
bg.drawString(text, 300, 200);
to replace lines 26 through 39.
kaciedy kaciedy

2012/10/24

#
That makes a lot of sense even if I would have never thought of it. Thank you so very very much.
You need to login to post a reply.