How to create a testArray in JUnit - java

I'm trying to test my program (TicTacToe) in several JUnit testcases. The methods I want to test are in the class "Board" (inside the same package):
Boolean notWon: Checks if there are three in a row in any of the three columns and rows or in one of two diagonals. Returns false if someone has won the game.
Boolean isBlank: (int int): Get's a position inside my two-dimensional board and checks if that position inhabits a value (-1 or 1) or is empty (0). If it is empty, return true.
Boolean notFull: This method checks if the board is completely filled, by going trough every position. If every position is !=0, return false.
My problem: I want to make a pre-defined "testBoard" which I fill with custom values in order to create certain game situations. How can I make a testBoard which is applicable for the external function calls? Calling these functions with the testBoard would yield a Boolean-value, which should be applicable for comparison (Assert.assertEquals).
I somehow get a NullPointerException when I run these Tests, which I do not understand as these Tests should use my pre-defined two dimensional array (testBoard).
BoardTest.class
package structurePack;
import org.junit.Assert;
import org.junit.Test;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class BoardTest {
int [][] testBoard;
/* Schema:
* (0,0) | (0,1) | (0,2)
* (1,0) | (1,1) | (1,2)
* (2,0) | (2,1) | (2,2)
*/
public int[][]createBoard(){
testBoard = new int[3][3];
testBoard[0][0] = 1;
testBoard[0][1] = -1;
testBoard[0][2] = 0;
testBoard[1][0] = 0;
testBoard[1][1] = 1;
testBoard[1][2] = 0;
testBoard[2][0] = 0;
testBoard[2][1] = -1;
testBoard[2][2] = 1;
return testBoard;
}
#Test
public void testNotWon() {
createBoard();
Assert.assertEquals(false, Board.notWon());
}
#Test
public void testNotWon2() {
createBoard();
Assert.assertEquals(true, Board.notWon());;
}
#Test
public void testIsBlank() {
createBoard();
Assert.assertEquals("Spot is not empty", false, Board.isBlank(0,0));
}
#Test
public void testIsBlank2() {
createBoard();
Assert.assertEquals("Spot is empty", true, Board.isBlank(0,2));
}
#Test
public void testNotFull() {
createBoard();
Assert.assertEquals("Board is not completely full", Board.notFull());
}
#Test
public void testNotFull2() {
createBoard();
Assert.assertEquals("Board is completely full", false, Board.notFull());
}
}
Board.class
package structurePack;
/**
* Created by Yassir on 27.04.2016.
*/
import java.util.Arrays;
import java.util.Scanner;
public class Board {
public static int[][] gameBoard;
public static char[][] consoleGameBoard;
private static boolean continueGame = true;
/**
* This is the constructor for the Board-Class
*/
public Board() {
gameBoard = new int[3][3];
consoleGameBoard = new char[3][3];
for (int row = 0; row < gameBoard.length; row++) {
Arrays.fill(gameBoard[row], 0);
}
} // end of constructor
public void displayBoard() {
for (int row = 0; row < consoleGameBoard.length; row++) {
for (int col = 0; col < consoleGameBoard.length; col++) {
System.out.print("\t" + consoleGameBoard[row][col]);
if (col == 0 || col == 1)
System.out.print("|");
}
System.out.print("\n_________________\n");
}
}
public static boolean updateBoard(int player, int row, int col) {
if (row >= 0 && row <= 2 && col >= 0 && col <= 2) {
if (gameBoard[row][col] != 0) {
return false;
} else {
gameBoard[row][col] = player;
return true;
}
} else {
return false;
}
}
public void userInput(int player) {
int row, col;
Scanner keyboard = new Scanner(System.in);
do {
System.out.printf("Player %s, please enter a row (1-3):", 'O');
row = keyboard.nextInt();
System.out.printf("Player %s, please enter a column (1-3):", 'O');
col = keyboard.nextInt();
} while (notValid(row, col));
updateBoard(player, row - 1, col - 1);
convertToChar(row - 1, col - 1);
}
public static boolean notValid(int row, int col) {
if (row > 3 || row < 1 || col > 3 || col < 1 || !isBlank(row, col)) {
return true;
} else {
return false;
}
}
public static boolean notFull() {
if (gameBoard[0][0] != 0 && gameBoard[0][1] != 0 && gameBoard[0][2] != 0 && gameBoard[1][0] != 0 && gameBoard[1][1] != 0 && gameBoard[1][2] != 0 &&
gameBoard[2][0] != 0 && gameBoard[2][1] != 0 && gameBoard[2][2] != 0) {
return false;
} else {
return true;
}
}
public static boolean isBlank(int row, int col) {
if (gameBoard[row - 1][col - 1] == 0) {
return true;
} else {
System.out.println(" -- INVALID! The Position is already taken! --");
System.out.println(" ------------- Make another Move -------------");
return false;
}
}
public static boolean notWon() {
// loop over each row and check for winner
for (int row = 0; row < gameBoard.length; row++) {
if (gameBoard[row][0] + gameBoard[row][1] + gameBoard[row][2] == 3 ||gameBoard[row][0] + gameBoard[row][1] + gameBoard[row][2] == -3) {
return continueGame = false;
}
}
// loop over each column and check for winner
for (int col = 0; col < gameBoard[0].length; col++) {
if (gameBoard[0][col] + gameBoard[1][col] + gameBoard[2][col] == 3 || gameBoard[0][col] + gameBoard[1][col] + gameBoard[2][col] == -3) {
return continueGame = false;
}
}
// check diagonal 1
if (gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2] == 3 || gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2] == -3) {
return continueGame = false;
}
// check for diagonal 2
if (gameBoard[2][0] + gameBoard[1][1] + gameBoard[0][2] == 3 ||gameBoard[2][0] + gameBoard[1][1] + gameBoard[0][2] == -3) {
return continueGame = false;
} else {
return continueGame = true;
}
}
public static void convertToChar(int row, int col) {
char X = 'X';
char O = 'O';
if (gameBoard[row][col] == 1) {
consoleGameBoard[row][col] = X;
} else {
consoleGameBoard[row][col] = O;
}
}
}

Before you proceed to my solution, read this:
Your code is poorly designed and is inappropriately using static. You should make Board "instantiable" and make most, if not all, of your members non-static. It does not only makes your code more testable, it also is how OOP should look like.
Go back to your problem. Given that your Board methods are accessing your Board's static gameBoard, and in your test, you have not set up Board.gameBoard (your createBoard() is not doing anything meaningful), hence it is giving you NPE when you call those methods.
Just change your createBoard() to something like
#Before
public void createBoard() {
Board.gameBoard = new int[][] { {0, 1, 1},
{-1, 0, 1},
{1, 0, -1} };
};
// and remove invocations of `createBoard()` in your test methods

Perhaps something like this:
private final Integer[][] testArray = {
{0, 1, 1},
{-1, 0, 1},
{1, 0, -1}
};

For better learning I would say look for Data Providers and their use in Unit Tests. You shall get a better approach to test your code.

Related

Java 2D Array Specific Move

I have made a class where a 6x10 2D array is generated to act as a board.
A random starting location is then generated in the constructor.I only want adjacent moves to be possible.
For example, if the random location has been generated as (2,3) then for example the user enters (1,2) it would be a valid move, but (6,1) would be an invalid move.
Then if the user enters say (1,2), they can then go to any adjacent cell from (1,2).
I have included the class below, and the adjacent method I tried to make to test it, but I'm a bit confused on how I am approaching this.
import java.util.Arrays;
import java.util.Random;
public class Test {
public static final int ROWS = 6;
public static final int COLUMNS = 10;
public int[][] board;
public static void main(String[] args)
{
Test t = new Test();
t.getBoard();
t.makeMove(6,1); //I want this to be an invalid move.
t.getBoard();
t.makeMove(1,2); // this should be a valid move
t.getBoard();
}
public Test()
{
board = new int[ROWS][COLUMNS];
createRandomLocation();
}
public void createRandomLocation()
{
Random rand = new Random();
int x = rand.nextInt(6);
int y = rand.nextInt(10);
board[x][y] = 1;
}
public void makeMove(int x,int y){
if (Math.abs(x-cur_x)==0 || Math.abs(y-cur_y)==0) {
board[x][y] = 1;
}
public String getBoard() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println();
return Arrays.deepToString(board);
}
}
Adjacent:
/*public boolean isMoveAllowed(int [][] array,int x, int y){
boolean adjacent = false;
int trueCount = 0;
if(array[x-1][y-1] == 0) trueCount++; //topleft
if(array[x-1][y] == 0) trueCount++; //top
if(array[x-1][y+1] == 0) trueCount++;//topright
if(array[x][y+1] == 0) trueCount++;//right
if(array[x][y-1] == 0) trueCount++;//left
if(array[x+1][y-1] == 0) trueCount++;//bottomleft
if(array[x+1][y] == 0) trueCount++;//bottom
if(array[x+1][y+1] == 0) trueCount++; //bottomright
if (trueCount == 8)
{
adjacent = true;
}
return adjacent;
}*/
Your problem description has the answer baked into it already. You want any move from (a,b) to (c,d) to be legal if the distance between a and c, and b and d, is zero or one. So if you see Math.abs(a-c)>1, that's an illegal move. So: have the current position stored in some variables, and compare them to the desired new location:
public static void main(String[] args)
{
Board b = new Board(6, 10);
try {
b.tryMove(6,1);
} catch(IllegalMoveException e) {
// do whatever you need to do to inform the user that move is illegal
}
}
With the Board class responsible for tracking coordinates:
class Board {
protected int cur_x, cur_y, rows, cols;
public Board(int rows, int cols) {
this.rows = rows;
this.cols = cols;
this.setRandomPosition();
}
public void setRandomPosition() {
cur_x = (int) Math.round(Math.random() * cols);
cur_y = (int) Math.round(Math.random() * rows);
}
public void tryMove(int x, int y) throws IllegalMoveException {
if (Math.abs(x-cur_x)>1 || Math.abs(y-cur_y)>1) {
throw new IllegalMoveException(...);
}
// bounds check omitted here, but: ensure that
// 0<=x<cols and 0<=y<rows, otherwise throw an
// IllegalMoveException as well.
cur_x = x;
cur_y = y;
}
// with getters for the current x and y, etc.
}
It would be much easier to test for a true case rather than a false case like you currently have, the isMoveAllowed method should look something like this:
public boolean isMoveAllowed(int[][] array, int x, int y) {
return ((array[x + 1][y] == 1) ||
(array[x - 1][y] == 1) ||
(array[x][y + 1] == 1) ||
(array[x][y - 1] == 1));
}
This will return true if the move is adjacent to the current player position

Constructing Mazes in java using 2D array and stacks

I need to construct a maze using a 2D array and stacks. The array size is fixed. the starting point is (0,0). The array should be read from a file but in this example, I am assuming values just to make things clear.
I can't seem to find a proper algorithm that lets me go through the 2D array and saves my path to the stack. And that gets me back to the upper row if I am stuck in the current row. PS: 1 is a wall and 0 is a path. The question requires an array input by the user but I provided one for simplicity
Heres the array:
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0
1 1 1 0 0
0 1 0 0 0
I need to start from position (0,0) and the exit should be in the last row. If I got stuck I need to go up and find another path; that is pop the stack.
Here's what I came up with:
public class Maze {
Maze currentPos = new Maze();
int position = maze[0][0];
public Maze()
{
}
public Maze(Maze currentPos)
{
this.currentPos = currentPos;
position = maze[0][0];
}
Stack stack = new Stack ();
public static int[][] maze = new int[][] {
{0,1,0,0,0},
{0,1,0,0,0},
{0,0,0,0,0},
{1,1,1,0,0},
{0,1,0,0,0}
};
public boolean UP (int i, int j)
{
if (maze [i-1][j] == 0)
return true;
return false;
}
public boolean DOWN (int i, int j)
{
if (maze [i+1][j] == 0)
return true;
return false;
}
public boolean RIGHT(int i,int j)
{
if (maze [i][j+1] == 0)
return true;
return false;
}
public boolean LEFT(int i,int j)
{
if (maze [i][j-1] == 0)
return true;
return false;
}
public boolean isExit (int i, int j)
{
if (j == 6)
return true;
return false;
}
public void setPosition(int i , int j)
{
position = maze[i][j];
}
public void solve()
{
for (int i=0; i<maze.length; i++)
{
for (int j=0; j<maze.length; j++)
{
while(! currentPos.isExit(i,j));
{
if ( currentPos.DOWN(i,j)) stack.push(i+1,j);
if ( currentPos.LEFT(i,j)) stack.push(i,j-1);
if ( currentPos.RIGHT(i,j)) stack.push(i,i+1);
if ( currentPos.UP(i,j)) stack.push(i-1,j);
}
}
}
}
}
The class stack is the same as the one in java.util.stack and with the same methods included (pop, push)
Here is something to get you started :
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Maze {
//keep reference to start point
int startRow, startCol;
//keep reference to addresses (row, col) that has been checked
List<Integer[]> visited;
//a stack that represents the path (solution)
Stack<Integer[]> path;
public Maze(int startRow, int startCol) {
this.startRow = startRow; //add: check input validity
this.startCol = startCol;
visited = new ArrayList<>();
path = new Stack<>();
}
public static int[][] mazeValues = new int[][] {
{0,1,0,0,0},
{0,0,0,1,0},
{1,1,1,0,0},
{1,1,1,0,1},
{0,0,0,0,0}
};
void solve(){
boolean isSolved = solve(startRow, startCol);
if( isSolved ) {
pathFound();
} else {
noPathFound();
}
}
private boolean solve(int row, int col) {
//check if target found
if(isTargert(row,col)) {
//add target to path
path.push(new Integer[]{row,col});
return true;
}
//check if address is a wall
if(isWall(row,col)) {
return false;
}
//check if visited before
if(isVisited(row, col)) {
return false;
}
//mark as visited
visited.add(new Integer[]{row,col});
//add to path
path.push(new Integer[]{row,col});
//check all neighbors (allows diagonal move)
for (int rowIndex = row-1; rowIndex <= (row+1) ; rowIndex++ ) {
for (int colIndex = col-1; colIndex <= (col+1) ; colIndex++ ) {
if( (rowIndex == row) && (colIndex == col)) {//skip current address
continue;
}
if( ! withInMaze(rowIndex, colIndex)) {
continue;
}
if( solve(rowIndex, colIndex)) {
return true; //solution found
}
}
}
//solution not found after checking all neighbors
path.pop(); //remove last from stack;
return false;
}
//check if address is a target
private boolean isTargert(int row, int col) {
//target set to last row / col. Change taget as needed
return (row == (mazeValues.length-1))&& (col == (mazeValues[0].length -1)) ;
}
//check if address is a wall
private boolean isWall(int row, int col) {
return mazeValues[row][col] == 1;
}
private boolean isVisited(int row, int col) {
for (Integer[] address : visited ) {
if((address[0]==row) && (address[1]==col)) {
return true;
}
}
return false;
}
//return true if rowIndex, colIndex are with in mazeValues
private boolean withInMaze(int rowIndex, int colIndex) {
return (rowIndex < mazeValues.length)&& (rowIndex >= 0)
&&(colIndex < mazeValues[0].length) && (colIndex >=0);
}
private void noPathFound() {
System.out.println("No path found............");
}
private void pathFound() {
System.out.println("Path found");
for (Integer[] address : path) {
int row = address[0]; int col = address[1];
System.out.println("Address: "+ row +"-"+ col
+" value: "+ mazeValues[row][col]);
}
}
public static void main(String[] args) {
Maze maze = new Maze(0,0);
maze.solve();
}
}
For generic maze path finding algorithms I would suggest to start with Breadth-first search

Stuck in a recursion maze

Need a push in the right direction for a class assignment. I've read other posts that mentioned creating a variable/method to store the path traveled, but not sure how to get about it...
Edited 9/28/16
was able to get to the end point of the maze but still haven't figured out
how to print only the path taken; I really need to
import java.io.*;
import java.util.*;
public class Maze
{
private static int rows, cols, startRow, startCol, nextRow, nextCol;
private static int endRow = 3;
private static int endCol = 34;
private static char[][] mazeBoard;
//private static char start = 'S';
private static char end = 'E';
//private boolean finish = false;
private char[][] explored = new char[rows][cols];
//construct the maze board
public Maze() throws FileNotFoundException
{
Scanner in = new Scanner(new File("maze.txt"));
rows = in.nextInt();
cols = in.nextInt();
startRow = in.nextInt();
startCol = in.nextInt();
//fill out the mazeBoard
mazeBoard = new char[rows][cols];
int i = 0;
while (in.hasNextLine())
{
String inLine = in.nextLine();
if (inLine.isEmpty())
{
continue;
}
for (int j = 0;j < cols; j++)
{
char nextChar = inLine.charAt(j);
mazeBoard[i][j] = nextChar;
System.out.print(nextChar);
}
System.out.println();
i++;
}
in.close();
}
//updated the move method from void to boolean
public boolean move(int row, int col, int prevRow, int prevCol)
{
boolean finish = false;
prevRow = row;
prevCol = col;
//show location
System.out.println("row: " + row + " col: " + col);
//base case1 to check for out of bounds and not the previous position
if (row < 0 || col < 0 || row >= rows || col >= cols || row != prevRow || col != prevCol)
{ return false; }
//base case2 to see if reached exit/end point
if (row == endRow && col == endCol)
{
System.out.println("Found the exit!");
return true;
}
//base case3 to check for wall
if (mazeBoard[row][col] == '+' || mazeBoard[row][col] == '*')
{ return false; }
mazeBoard[row][col] = '*';
//try to move down
if (move(row + 1, col, prevRow, prevCol))
{ return true; }
//try to move right
if (move(row, col + 1, prevRow, prevCol))
{ return true; }
//try to move up
if (move(row - 1, col, prevRow, prevCol))
{ return true; }
//try to move left
if (move(row, col - 1, prevRow, prevCol))
{ return true; }
row = prevRow;
col = prevCol;
return false;
}
public static void main(String[] args) throws FileNotFoundException
{
Maze maze = new Maze();
maze.move(startRow, startCol);
}
}
====
so I'm not sure how to implement a method to keep track of path traveled, any pointers will be greatly appreciated!
The easy way is to wait until you find the solution. Then simply record the successful moves as you crawl back up that branch of the call tree. Each winning call prepends its move to the front of the return value and passes that back up the stack. This would be something like
result = move(rowM + 1, colM);
if result != ""
return "D" + result; // "D" for a move right
else {
// Try a move right ...
You do have a couple of things to fix. Most of all, you have to block taking a step you've already taken. Right now, when your search hits a dead end, it keeps repeating the final step and backtrack in an infinite recursion.
Second, you'll need to implement logic to abort other searches once you've found one solution. Setting a finish doesn't help much; that's a local variable, and you need to communicate to the calling program that you've failed or succeeded.
Is that enough to move you to the next step?

Arrays for Maze Algorithm not Storing Proper Values

I have everything down in my maze solver, except for the fact that the wasHere array is storing the solution (which is supposed to be stored by the correctPath array). It is also missing marking the end square of the maze. All the wasHere array is supposed to store are the spots that the program has gone to in the maze. The correctPath array has all false values, which is totally unexpected. I am using the recursive method mentioned in Wikipedia: https://en.wikipedia.org/wiki/Maze_solving_algorithm
This is my Maze Solver:
private static int[][] maze = {{2, 2, 2, 2, 1, 2, 2},
{2, 2, 2, 2, 1, 2, 2},
{2, 2, 2, 2, 1, 2, 2},
{2, 1, 1, 1, 1, 1, 1}}; // The maze
private static boolean[][] wasHere = new boolean[4][7];
private static boolean[][] correctPath = new boolean[4][7]; // Solution
private static int startX = 4;
private static int startY = 0;
private static int endX = 1;
private static int endY = 3;
public static void main(String[] args) {
System.out.println("Maze: ");
printMaze(maze);
solveMaze();
boolean b = recursiveSolve(startX, startY); // Whether or not there is a solution to the maze
}
public static void solveMaze()
{
for (int row = 0; row < maze.length; row++)
{
// Sets boolean arrays to false
for (int col = 0; col < maze[row].length; col++)
{
wasHere[row][col] = false;
correctPath[row][col] = false;
}
}
}
public static void printMaze(int[][] array)
{
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
System.out.print(array[row][col]);
if (col == array[row].length - 1)
{
System.out.print("\n");
}
}
}
System.out.print("\n");
}
public static void printPath(boolean[][] array)
{
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[row].length; col++)
{
if (array[row][col] == true)
{
System.out.print("1");
}
else
{
System.out.print("2");
}
if (col == array[row].length - 1)
{
System.out.print("\n");
}
}
}
}
public static boolean recursiveSolve(int x, int y)
{
if (x == endX && y == endY) // Reach end
{
System.out.println("The maze is solvable.");
printPath(wasHere);
return true;
}
if (maze[y][x] == 2 || wasHere[y][x] == true) // Hit a dead end or end up in same place (no solution)
{
return false;
}
wasHere[y][x] = true;
if (x != 0) // On left edge or not
{
if (recursiveSolve(x - 1, y))
{
correctPath[y][x] = true;
return true;
}
}
if (x != maze[0].length - 1) // On right edge or not
{
if (recursiveSolve(x + 1, y))
{
correctPath[y][x] = true;
return true;
}
}
if (y != 0) // On top edge or not
{
if (recursiveSolve(x, y - 1))
{
correctPath[y][x] = true;
return true;
}
}
if (y != maze.length - 1) // On bottom edge or not
{
if (recursiveSolve(x, y + 1))
{
correctPath[y][x] = true;
return true;
}
}
System.out.println("The maze is not solvable.");
return false;
}
Your maze solver is working correctly. The problem is that you were probably printing the values of the correctPath array before your recursive method had finished writing to it.
I assume that where you had the following lines inside the recursiveSolve(int x, int y) method:
System.out.println("The maze is solvable.");
printPath(wasHere);
... at some point, you tried to run it using the correctPath variable instead, right? Something like this?
System.out.println("The maze is solvable.");
printPath(correctPath);
But that is too soon. The correctPath array values are set after the recursive calls start returning from the end of the maze.
Instead, try moving the printPath call after the top level call to the recursiveSolve method inside your main(). Like this:
public static void main(String[] args) {
System.out.println("Maze: ");
printMaze(maze);
solveMaze();
boolean b = recursiveSolve(startX, startY); // Whether or not there is a solution to the maze
// Put this here! It will work as expected.
System.out.println();
printPath(correctPath);
}
If this doesn't quite make sense to you, then it probably means that you haven't quite grasped how recursion works. Use a debugger to step through your program, as you should have done in the first place, and things should become clearer.

Array programming - check winner in a Tic Tac Toe game for an nxn board with n players

I am making a tic tac toe game for n number of players on a nxn board, but the winning condition is aways 3 on a row. My so far solution to the problem is: when a move is made the program will check the following square for 3 on a row.
(x-1,y+1) (x,y+1) (x+1,y+1)
(x-1,y) (x,y) (x+1,y)
(x-1,y-1) (x,y-1) (x+1,y-1)
It will check the top (x-1,y+1) (x,y+1) (x+1,y+1) bottom(x-1,y-1) (x,y-1) (x+1,y-1)
sides(x+1,y+1) (x+1,y) (x+1,y-1) , (x-1,y+1) (x-1,y) (x-1,y-1) , the diagonals and the ones going through the middle(x,y).
my code so far is:
public int checkWinning() {
for(int a = 1; a < size-1; a++){
for(int b = 1; b < size-1; b++){
if (board[a][b] == board[a+1][b] && board[a][b] == board[a-1][b]){
return board[a][b];
}else if(board[a][b] == board[a][b+1] && board[a][b] == board[a][b-1]){
return board[a][b];
}else if(board[a][b] == board[a+1][b-1] && board[a][b] == board[a-1][b+1]){
return board[a][b];
}else if(board[a][b] == board[a+1][b+1] && board[a][b] == board[a-1][b-1]){
return board[a][b];
}
}
}
for(int d = 1; d < size-1; d++){
if (board[0][d] == board[0][d-1] && board[0][d] == board[0][d+1]){
return board[0][d];
} else if (board[size-1][d] == board[size-1][d-1] && board[size-1][d] == board[size-1][d+1]){
return board[size-1][d];
}
}
for(int c = 1; c < size-1; c++){
if (board[c][0] == board[c-1][0] && board[c][0] == board[c+1][0]){
return board[c][0];
}else if(board[c][size-1] == board[c-1][size-1] && board[c][size-1] == board[c+1][size-1]){
return board[c][size-1];
}
}
return 0;
}
where the first section is where I check the ones through the middle and diagonals. the second section I check the top an bottom and the top and the thrid section checks the sides.
When it returns 0 is means that there are no winner yet.
#override
public void checkResult() {
int winner = this.board.checkWinning();
if (winner > 0) {
this.ui.showResult("Player "+winner+" wins!");
}
if (this.board.checkFull()) {
this.ui.showResult("This is a DRAW!");
}
}
Board[x][y] -> 2-dimensional array representing the board, The coordinates are counted from top-left (0,0) to bottom-right (size-1, size-1), board[x][y] == 0 signifies free at position (x,y), board[x][y] == i for i > 0 signifies that Player i made a move on (x,y), just so you know it.
my problem is that when i expands the board to a size larger than 3x3 the program somehow overwrites it self or a does not check every thing sides top and bottom every time, and I can't seem too se why.
EDIT:
played with the app for a few minutes... interesting results
java -jar tic-tac-toe.jar 5 20
It was a cats game!!
|1|1|5|5|1|3|5|3|1|5|2|5|1|1|2|
|2|3|2|3|1|5|3|5|3|2|3|1|5|2|2|
|5|4|5|4|1|5|5|4|2|1|4|5|4|2|2|
|3|2|1|5|5|5|2|4|5|3|4|1|2|4|2|
|3|4|1|2|5|4|1|1|4|5|1|3|3|4|1|
|1|5|4|4|3|2|5|1|3|5|1|3|5|3|4|
|2|5|1|4|3|3|3|5|3|1|1|4|3|4|4|
|1|4|5|1|1|5|4|5|2|4|1|1|5|4|3|
|1|3|2|1|4|2|4|3|3|4|5|2|4|3|3|
|5|1|1|3|3|4|4|4|2|2|1|4|3|2|5|
|2|2|3|1|5|5|4|1|3|5|3|2|3|3|2|
|2|4|2|4|4|1|3|1|1|3|1|2|1|2|2|
|2|5|5|1|4|3|4|5|5|4|5|3|3|5|2|
|4|5|2|1|5|3|2|1|3|2|2|2|2|4|4|
|4|1|1|4|5|4|5|4|2|2|3|3|2|2|3|
Played 100 games:
Number wins by Player1: 0
Number wins by Player2: 0
Number wins by Player3: 0
Number wins by Player4: 0
Number wins by Player5: 0
Number of ties: 100
didn't scroll through all 100 games to find the winning board, but I thought this was interesting:
java -jar tic-tac-toe.jar 2 10
Player2 won the game!
|1|1|2|1|2|2| |2|1|2|
|2|2|2|2|2|2|2|2|2|2|
|2|1|2|2|2|1|1|1|1|1|
|1|1|1|1|2|1|2|1|1|1|
|2|2| |1|2|1|1|1|1|2|
|2|2|2|1|1|1| |1|2|2|
|2|2|1|2|2|2|2|2|1|1|
| | |2|2|2|2| |1|1|1|
|1|1|2|2|2|1|1|1|1| |
| | |1|1|1|1|1|2|1| |
Played 100 games:
Number wins by Player1: 0
Number wins by Player2: 1
Number of ties: 99
This does answer your question... but I took it a bit far... decided to implement the solution.
Instead of counting matches... I just check from teh point the last player plays, if all marks in a row column and diagnal match the players, he wins.
package com.clinkworks.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TicTacToe {
private static final String TIE = "TIE";
private static final Map<String, Integer> gamesToWinsMap = new HashMap<String, Integer>();
/**
* accepts input in the following format:
*
* playerCount rowCount columnCount (sets the game with the n players, n columns, and n rows)
* - java -jar tic-tac-toe.jar 2 3 3
* PlayerCount squareSize (defaults to a game with rows and cols the same as squareSize and the player count given)
* - java -jar tic-tac-toe.jar 2 3
* PlayerCount (defaults to a 3 by 3 game)
* - java -jar tic-tac-toe.jar 2
* no input (defaults to a 3 by 3 game with 2 players)
* - java -jar tic-tac-toe.jar
* #param args
*/
public static void main(String[] args) {
int playerCount = 2;
int rows = 3;
int cols = 3;
if(args.length == 3){
playerCount = Integer.valueOf(args[0]);
rows = Integer.valueOf(args[1]);
cols = Integer.valueOf(args[2]);
}
if(args.length == 2){
playerCount = Integer.valueOf(args[0]);
rows = Integer.valueOf(args[1]);
cols = rows;
}
if(args.length == 1){
playerCount = Integer.valueOf(args[0]);
}
for(int i = 1; i <= playerCount; i++){
gamesToWinsMap.put("Player" + i, 0);
}
//lets play 100 games and see the wins and ties
playGames(100, playerCount, rows, cols);
for(int i = 1; i <= playerCount; i++){
System.out.println("Number wins by Player" + i + ": " + gamesToWinsMap.get("Player" + i));
}
System.out.println("Number of ties: " + gamesToWinsMap.get(TIE));
}
public static void playGames(int gamesToPlay, int playerCount, int rows, int cols) {
//play a new game each iteration, in our example, count = 100;
for (int i = 0; i < gamesToPlay; i++) {
playGame(playerCount, rows, cols);
}
}
public static void playGame(int playerCount, int rows, int cols) {
//create a new game board. this initalizes our 2d array and lets the complexity of handling that
// array be deligated to the board object.
Board board = new Board(playerCount, rows, cols);
//we are going to generate a random list of moves. Heres where we are goign to store it
List<Move> moves = new ArrayList<Move>();
//we are creating moves for each space on the board.
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
moves.add(new Move(row, col));
}
}
//randomize the move list
Collections.shuffle(moves);
//do each move
for (Move move : moves) {
board.play(move);
if(gameOver(board)){
break;
}
}
}
public static boolean gameOver(Board board){
if (board.whoWon() != null) {
System.out.println(board.whoWon() + " won the game!");
System.out.println(board);
Integer winCount = gamesToWinsMap.get(board.whoWon());
winCount = winCount == null ? 1 : winCount + 1;
gamesToWinsMap.put(board.whoWon(), winCount);
return true;
} else if (board.movesLeft() == 0) {
System.out.println("It was a cats game!!");
System.out.println(board);
Integer tieCount = gamesToWinsMap.get(TIE);
tieCount = tieCount == null ? 1 : tieCount + 1;
gamesToWinsMap.put(TIE, tieCount);
return true;
}
return false;
}
public static class Move {
private int row;
private int column;
public Move(int row, int column) {
this.row = row;
this.column = column;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
public static class Board {
private final int rowSize;
private final int columnSize;
private final Integer[][] gameBoard;
private int playerCount;
private int currentPlayer;
private String winningPlayer;
public Board() {
gameBoard = new Integer[3][3];
currentPlayer = 1;
winningPlayer = null;
this.rowSize = 3;
this.columnSize = 3;
playerCount = 2;
}
public Board(int players) {
gameBoard = new Integer[3][3];
currentPlayer = 1;
winningPlayer = null;
this.rowSize = 3;
this.columnSize = 3;
playerCount = players;
}
public Board(int rowSize, int columnSize) {
gameBoard = new Integer[rowSize][columnSize];
currentPlayer = 1;
winningPlayer = null;
playerCount = 2;
this.rowSize = rowSize;
this.columnSize = columnSize;
}
public Board(int players, int rowSize, int columnSize) {
gameBoard = new Integer[rowSize][columnSize];
currentPlayer = 1;
winningPlayer = null;
playerCount = players;
this.rowSize = rowSize;
this.columnSize = columnSize;
}
/**
*
* #return the amount of empty spaces remaining on the game board, or if theres a winning player, zero.
*/
public int movesLeft() {
if(whoWon() != null){
return 0;
}
int moveCount = 0;
for (int x = 0; x < getRowSize(); x++) {
for (int y = 0; y < getColumnSize(); y++) {
moveCount += getMoveAt(x, y) == null ? 1 : 0;
}
}
return moveCount;
}
/**
* If someone won, this will return the winning player.
*
* #return the winning player
*/
public String whoWon() {
return winningPlayer;
}
/**
* This move allows the next player to choose where to place their mark.
*
* #param row
* #param column
* #return if the game is over, play will return true, otherwise false.
*/
public boolean play(Move move) {
if (!validMove(move)) {
// always fail early
throw new IllegalStateException("Player " + getCurrentPlayer() + " cannot play at " + move.getRow() + ", " + move.getColumn() + "\n" + toString());
}
doMove(move);
boolean playerWon = isWinningMove(move);
if (playerWon) {
winningPlayer = "Player" + getCurrentPlayer();
return true;
}
shiftPlayer();
boolean outOfMoves = movesLeft() <= 0;
return outOfMoves;
}
public int getRowSize() {
return rowSize;
}
public int getColumnSize() {
return columnSize;
}
public int getCurrentPlayer() {
return currentPlayer;
}
public Integer getMoveAt(int row, int column) {
return gameBoard[row][column];
}
private void doMove(Move move) {
gameBoard[move.getRow()][move.getColumn()] = getCurrentPlayer();
}
private void shiftPlayer() {
if(getCurrentPlayer() == getPlayerCount()){
currentPlayer = 1;
}else{
currentPlayer++;
}
}
private int getPlayerCount() {
return playerCount;
}
private boolean validMove(Move move) {
boolean noMoveAtIndex = false;
boolean indexesAreOk = move.getRow() >= 0 || move.getRow() < getRowSize();
indexesAreOk = indexesAreOk && move.getColumn() >= 0 || move.getColumn() < getColumnSize();
if (indexesAreOk) {
noMoveAtIndex = getMoveAt(move.getRow(), move.getColumn()) == null;
}
return indexesAreOk && noMoveAtIndex;
}
private boolean isWinningMove(Move move) {
// since we check to see if the player won on each move
// we are safe to simply check the last move
return winsDown(move) || winsAcross(move) || winsDiagnally(move);
}
private boolean winsDown(Move move) {
boolean matchesColumn = true;
for (int i = 0; i < getColumnSize(); i++) {
Integer moveOnCol = getMoveAt(move.getRow(), i);
if (moveOnCol == null || getCurrentPlayer() != moveOnCol) {
matchesColumn = false;
break;
}
}
return matchesColumn;
}
private boolean winsAcross(Move move) {
boolean matchesRow = true;
for (int i = 0; i < getRowSize(); i++) {
Integer moveOnRow = getMoveAt(i, move.getColumn());
if (moveOnRow == null || getCurrentPlayer() != moveOnRow) {
matchesRow = false;
break;
}
}
return matchesRow;
}
private boolean winsDiagnally(Move move) {
// diagnals we only care about x and y being teh same...
// only perfect squares can have diagnals
// so we check (0,0)(1,1)(2,2) .. etc
boolean matchesDiagnal = false;
if (isOnDiagnal(move.getRow(), move.getColumn())) {
matchesDiagnal = true;
for (int i = 0; i < getRowSize(); i++) {
Integer moveOnDiagnal = getMoveAt(i, i);
if (moveOnDiagnal == null || moveOnDiagnal != getCurrentPlayer()) {
matchesDiagnal = false;
break;
}
}
}
return matchesDiagnal;
}
private boolean isOnDiagnal(int x, int y) {
if (boardIsAMagicSquare()) {
return x == y;
} else {
return false;
}
}
private boolean boardIsAMagicSquare() {
return getRowSize() == getColumnSize();
}
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
for(int y = 0; y < getColumnSize(); y++) {
for(int x = 0; x < getRowSize(); x++) {
Integer move = getMoveAt(x, y);
String moveToPrint = "";
if (move == null) {
moveToPrint = " ";
} else {
moveToPrint = move.toString();
}
stringBuffer.append("|").append(moveToPrint);
}
stringBuffer.append("|\n");
}
return stringBuffer.toString();
}
}
}
I have to revise my answer. If you want to have three in a row regardless of your board size, your loop code might be sufficient, but you are always checking whether the values of the fields are the same but never make a difference between empty and non-empty fields.
So “empty” can win too, which would effectively hide a possible win of a player. In other words, your code does not work correctly, even for a field size of three. You didn’t test it enough.
If I initialize the board as
int[][] board={
{ 1, 1, 1 },
{ 0, 0, 0 },
{ 0, 0, 0 },
};
your code returns 0 as the second row contains three zeros. I assumed that 0 represents the empty field but the actual value for “empty” doesn’t matter. You have to exclude empty fields from the three-in-a-row check.
You can simplify this a fair amount by breaking the logic up a bit.
First realize that you only need to check for a win around the piece you just placed.
Now we need a way to check whether that move is a winner.
First we need a simple function to check whether a cell matches a given value, returning true if its within bounds and matches.
private boolean cellMatches(int x, int y, int val) {
if (x<0||x>boardWidth)
return false;
if (y<0||y>boardHeight)
return false;
return board[x][y]==val;
}
Now a function that you give a starting position (x and y) and a delta (dx, dy) and it checks up to two cells in that direction returning a count of how many in a row matched value. The for loop may be overkill for two checks but it would easily allow you to expand up to longer lines being used.
private int countMatches(int x, int y, int dx, int dy, int val) {
int count = 0;
for (int step=1;step<=2;step++) {
if (cellMatches(x+dx*step, y+dy*step, val) {
count++;
} else {
return count;
}
}
return count;
}
Now we can use the previous method. When we place a new piece we can just count out in each matching pair of directions. The combined count is the total number in a row. (i.e. two in a row top + 1 bot = a total run length of 4). If any of those run lengths is three then it is a winning move.
private boolean makeMove(int x, int y, int val) {
board[x][y] = val;
int runlength=countMatches(x,y,0,1,val) + countMatches(x,y,0,-1,val);
if (runLength >= 2)
return true;
int runlength=countMatches(x,y,1,0,val) + countMatches(x,y,-1,0,val);
if (runLength >= 2)
return true;
int runlength=countMatches(x,y,1,1,val) + countMatches(x,y,-1,-1,val);
if (runLength >= 2)
return true;
int runlength=countMatches(x,y,1,-1,val) + countMatches(x,y,-1,1,val);
if (runLength >= 2)
return true;
return false;
}
Note that because we need to count the center piece that we placed we just need a run length of two or more.

Categories

Resources