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;
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 | 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 ); } } |