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