Hey guy,
I'm making a Tic-tac-toe game. I plan on using an array to store the placement of X's and O's. I have that working and all, but i need to a way to check the array so i can setup the player winning. I'm also up to suggestions, the way i plan on doing it will require 16 if statements and with there is something for efficient, I'll take it.
Here is the coding I have for far.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Play da tic tac and toe * * @author Lukas Armstrong * 11/12/2014 */ public class Board extends World { private boolean t; public String board[][]={{" "," "," "},{" "," "," "},{" "," "," "}}; public Board() { super(3, 3, 100); t=true; GreenfootImage bg = getBackground(); bg.setColor(Color.BLACK); bg.drawLine(100,0, 100, 300); bg.drawLine(200,0, 200, 300); bg.drawLine(0,100, 300, 100); bg.drawLine(0, 200, 300, 200); } public void act() { //reset if (Greenfoot.isKeyDown("F2")) { Greenfoot.setWorld(new Board()); } //decide players if (t) // Even { Player1(); } else // Odd { Player2(); } //detect winning if ( { } } private void Player1() { if (Greenfoot.mousePressed(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); if (board[mouse.getX()][mouse.getY()].equals(" ")) { addObject(new x(), mouse.getX(), mouse.getY()); board[mouse.getX()][mouse.getY()]="x"; } t=false; } } private void Player2() { if (Greenfoot.mousePressed(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); if (board[mouse.getX()][mouse.getY()].equals(" ")) { addObject(new o(), mouse.getX(), mouse.getY()); board[mouse.getX()][mouse.getY()]="o"; } t= true; } } }