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

2014/11/13

How to check an array?

GhostTheToast GhostTheToast

2014/11/13

#
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;
        }
    }
}
danpost danpost

2014/11/15

#
There are most probably many ways to make what you have already more efficient; however, that is something you can learn and deal with at some later time. For now, let us work on the checking to see if a three-in-a-row has been scored. There are only 8 ways to score a three-in-a-row; so I cannot see where you would need 16 'if' statements (actually, they can all be combined into a single 'if' statement). Just as an example, not necessarily for you to use directly:
1
2
3
if (isWinner(0, 1, 2) || isWinner(3, 4, 5) || isWinner(6, 7, 8) ||
    isWinner(0, 3, 6) || isWinner(1, 4, 7) || isWinner(2, 5, 8) ||
    isWinner(0, 4, 8) || isWinner(2, 4, 6))
this can be used, where each square in the grid is numbered and the method 'isWinner', which would need written to return a boolean value after checking the given array elements for equal values, not being " ". You should be able to come up with something similar with what you started with (actually, it is possible to use the 'if' statement above, as is, in your scenario; but, the 'isWinner' method would have to convert the numbers given to the proper indices for use with your array).
You need to login to post a reply.