Cell life Game java - java

Hello i am designing a basic java "game" where you put in a starting true point the length and size of array
then the program will check which value is true and it has to follow the next set of rules
if the cell is currently occupied, it remains occupied only if exactly one neighbour is occupied;
it the cell is currently empty, it remains empty only if both neighbours are empty
the code that i have so far kinda works but it puts a true too soon in the beginning which causes rule 1 to not work
import java.util.*;
class Cell
{
public static void main (String[] args)
{int l;
int g; //number of the generation
String s, a;//stands for automata
int p; //position of the true cells
int currentG; //current generation
Scanner scanner;
scanner= new Scanner(System.in);
a=scanner.next();
l=scanner.nextInt();
g=scanner.nextInt();
s=scanner.next();
boolean[][] cellsCurrent = new boolean[g][l+2];
currentG=0;
while(scanner.hasNextInt()){ //put the position values in an array
p=scanner.nextInt();
if(p<=l){
cellsCurrent[currentG][p] = true;
}
}
s=scanner.next();//ik weet niet echt wat ik anders met die *init_end* moet
if(a.equals("A")){
for(currentG=0; currentG<g-1; currentG++){ //for all generations
for(int i=1; i<l+1; i++){ //for all cells
if(cellsCurrent[currentG][i] == true && ((cellsCurrent[currentG][i+1] == true && cellsCurrent[currentG][i-1] == false)||(cellsCurrent[currentG][i+1] == false && cellsCurrent[currentG][i-1] == true ))){ //dit werkt dus nog niet
cellsCurrent[currentG+1][i] = true;
}
else {if (cellsCurrent[currentG][i] == true){
cellsCurrent[currentG+1][i] = false;
}}
if(cellsCurrent[currentG][i] == false && cellsCurrent[currentG][i+1] == false && cellsCurrent[currentG][i-1] == false){
cellsCurrent[currentG+1][i] = false;
}
else{
cellsCurrent[currentG+1][i] = true;
}
}
}
}
for (int i = 0; i < cellsCurrent.length; i++) {
System.out.println(Arrays.toString(cellsCurrent[i]).replace("true", "*")
.replace("false", " "));
}
}
}

You can increase your chances of success with cleaner code. Use an extra method like this:
static int getLivingNeighbors (boolean[] arr, int index) {
int result = 0;
if (index > 0 && arr[index-1])
result++;
if (index < arr.length-1 && arr[index+1])
result++;
return result;
}
Using this will help you to get a clearer view on what you really want to do.
You can call it roughly like this: getLivingNeighbors(cellsCurrent[currentG],i)

Related

Stack Overflow with recursion in Java

I'm trying to write an AI that never loses at Tic Tac Toe, and I want to use the minimax algorithm to do so. However, when I try to run the program, a stack overflow appears and I can't seem to find what is the error. Could you take a look and tell me what I'm doing wrong? It doesn't go as deep in the recursion I believe, since it should only go through all the possible game outcomes, which go up to 8 moves (since the player is first to play, not the AI). It is probably me doing something wrong, but I can't find anything.
EDIT: Here's the full code, the mechanics function is the main part:
EDIT2: Fixed the constructor
package Packet;
import java.util.*;
import java.util.Scanner;
public class Logic {
public static class TicTacToe{
private int[] currentBoard = new int[9];
private int[] availableSpots = new int [9];
private int emptySpace = 0;
private int playerAI = 1;
private int playerHuman = 2;
void TicTacToe(){
for (int i = 0; i < 9; i++){
this.currentBoard[i] = this.emptySpace;
}
for (int i = 0; i < 9; i++){
this.availableSpots[i] = i;
}
}
private int movesNumber(){
int counter = 0;
for (int i = 0; i < 9; i++){
if (this.currentBoard[i] == this.emptySpace){
counter++;
}
}
return counter;
}
private boolean win(int[] board,int player){
if (
(board[0] == player && board[1] == player && board[2] == player) ||
(board[3] == player && board[4] == player && board[5] == player) ||
(board[6] == player && board[7] == player && board[8] == player) ||
(board[0] == player && board[3] == player && board[6] == player) ||
(board[1] == player && board[4] == player && board[7] == player) ||
(board[2] == player && board[5] == player && board[8] == player) ||
(board[0] == player && board[4] == player && board[8] == player) ||
(board[2] == player && board[4] == player && board[6] == player) ){
return true;
}
else{
return false;
}
}
private int mechanics(int[] newBoard, int player){
if (win(newBoard,this.playerHuman)){
return -10;
}
else if (win(newBoard, this.playerAI)){
return +10;
}
else if (this.movesNumber() == 0){
return 0;
}
ArrayList<Integer> moves = new ArrayList<Integer>();
ArrayList<Integer> scores = new ArrayList<Integer>();
for (int i = 0; i < this.movesNumber(); i++){
int[] possibleBoard = new int[9];
possibleBoard = newBoard;
int availableSpotNumber = i;
int j = i;
while (this.availableSpots[j] == 9){
availableSpotNumber++;
j++;
}
possibleBoard[availableSpotNumber] = player;
if (player == this.playerAI){
scores.add(this.mechanics(possibleBoard, this.playerHuman));
}
else{
scores.add(this.mechanics(possibleBoard, this.playerAI));
}
moves.add(availableSpotNumber);
possibleBoard[availableSpotNumber] = this.emptySpace;
}
int bestMove = 0;
if (player == this.playerAI){
int bestScore = -10000;
for (int i = 0; i < moves.size(); i++){
if (scores.get(i) > bestScore){
bestScore = scores.get(i);
bestMove = i;
}
}
}
else {
int bestScore = 10000;
for (int i = 0; i < moves.size(); i++){
if (scores.get(i) < bestScore){
bestScore = scores.get(i);
bestMove = i;
}
}
}
return moves.get(bestMove);
}
public void printTable(){
System.out.println(this.currentBoard[0] + " | " + this.currentBoard[1] + " | " + this.currentBoard[2]);
System.out.println("- - -");
System.out.println(this.currentBoard[3] + " | " + this.currentBoard[4] + " | " + this.currentBoard[5]);
System.out.println("- - -");
System.out.println(this.currentBoard[6] + " | " + this.currentBoard[7] + " | " + this.currentBoard[8]);
System.out.println();
}
private void fillTable(int position,int player){
this.currentBoard[position] = player;
this.availableSpots[position] = 9;
}
public void startGame(){
while(true){
this.printTable();
Scanner ulaz = new Scanner(System.in);
fillTable(ulaz.nextInt(), this.playerHuman);
this.printTable();
fillTable(this.mechanics(this.currentBoard, this.playerAI), this.playerAI);
ulaz.close();
}
}
public void resetGame(){
for (int i = 0; i < 9; i++){
this.currentBoard[i] = this.emptySpace;
}
for (int i = 0; i < 9; i++){
this.availableSpots[i] = i;
}
}
}
public static void main(String[] args){
TicTacToe game = new TicTacToe();
game.startGame();
}
}
Also, here's the exact errors I get:
Exception in thread "main" java.lang.StackOverflowError
at Packet.Logic$TicTacToe.mechanics(Logic.java:54)
at Packet.Logic$TicTacToe.mechanics(Logic.java:84)
at Packet.Logic$TicTacToe.mechanics(Logic.java:87)
at Packet.Logic$TicTacToe.mechanics(Logic.java:84)
at Packet.Logic$TicTacToe.mechanics(Logic.java:87)
at Packet.Logic$TicTacToe.mechanics(Logic.java:84)
at Packet.Logic$TicTacToe.mechanics(Logic.java:87)
After this part, these parts appear a bunch of times (at least 50)
at Packet.Logic$TicTacToe.mechanics(Logic.java:84)
at Packet.Logic$TicTacToe.mechanics(Logic.java:87)
Line 54:
if (win(newBoard,this.playerHuman)){
Line 84:
scores.add(this.mechanics(possibleBoard, this.playerHuman));
Line 87:
scores.add(this.mechanics(possibleBoard, this.playerAI));
This might or might not be a code issue, as Java is not a fully functional language with it comes to recursion, you might want to see this answer: Does Java 8 have tail call optimization?
Basically, a language that allows for unlimited-depth recursion has to have tail recursion optimization. With tail call optimization, if the return value is the result of exactly the same function with different parameters, the stack will get replaced instead of the new call being added to the stack.
If a language does not have tail call optimization, then you're limited by stack size in how deep your recursion goes, even if the recursive calls have correct termination conditions (note: I haven't analyzed the code in depth, so obviously there might be a problem in the recursive logic itself). If you want to tweak the stack size, use the -Xss Java runtime parameter. Generally, increasing the stack size is a good heuristic (although not a fool-proof method) of checking whether the fault is with the language or with your algorithm.
There's a couple of problems, but here's how you can debug this:
add a StringBuilder debug = new StringBuilder(); field to your class, then change the main loop like this:
int debugLen = debug.length();
debug.append("\nSetting ").append(availableSpotNumber).append(" to ").append(player);
possibleBoard[availableSpotNumber] = player;
try {
if (player == this.playerAI) {
scores.add(this.mechanics(possibleBoard, this.playerHuman));
} else {
scores.add(this.mechanics(possibleBoard, this.playerAI));
}
moves.add(availableSpotNumber);
} catch (StackOverflowError error) {
throw new StackOverflowError(debug.toString());
}
debug.setLength(debugLen);
possibleBoard[availableSpotNumber] = this.emptySpace;
Then you will see what is happening, which will give you a clue what to fix next. For example, the current version is doing this, for initial human move 1:
Setting 0 to 1
Setting 0 to 2
Setting 0 to 1
Setting 0 to 2
etc..
But, if you're too lazy, you can find a fixed version here.

Can't seem to get my method (in Java) to compile correctly

I am trying to figure out exactly what is wrong with my winorTie method in this little tictacttoe game I am trying to create. Would anyone be able to help? Thanks
package tictactoegame;
/**
*
* #author Douglas Boulden
*/
public class tictactoegame {
static int [][] gameboard;
static final int EMPTY = 0;
static final int NOUGHT = -1;
static final int CROSS = 1;
static void set (int val, int row) throws IllegalArgumentException {
int col = 0;
if (gameboard[row][col] == EMPTY)
gameboard[row][col] = val;
else throw new
IllegalArgumentException("Player already there!");
}
static void displayBoard () {
for (int[] gameboard1 : gameboard) {
System.out.print("|");
for (int c = 0; c < gameboard1.length; c++) {
switch (gameboard1[c]) {
case NOUGHT:
System.out.print("0");
break;
case CROSS:
System.out.print("X");
break;
default: //Empty
System.out.print(" ");
}
System.out.print("|");
}
System.out.println("\n------\n");
}
}
static void createBoard(int rows, int cols) {
gameboard = new int [rows] [cols];
}
static int winOrTie() {
if (gameboard [0][0] == NOUGHT && gameboard [0][-1])
return NOUGHT;
} else if (gameboard [0][0] == && CROSS) [0][1] {
return CROSS;
} else if (gameboard [0][0]== && " "()) [0][0] {
return 0;
} else {
return false;
}
/**
* #param args the command line arguments
*/ /**
* #param args the command line arguments
*/
public static void main(String[] args) {
createBoard(3,3);
int turn = 0;
int playerVal;
int outcome;
java.util.Scanner scan = new
java.util.Scanner(System.in);
do {
displayBoard();
playerVal = (turn % 2 == 0)? NOUGHT : CROSS;
if (playerVal == NOUGHT) {
System.out.println ("\n-0's turn-");
} else {
System.out.println("\n-X's turn-");
}
System.out.print("Enter row and Column:");
try {
set(playerVal, scan.nextInt());
} catch (IllegalArgumentException ex)
{System.err.println(ex);}
turn ++;
outcome = winOrTie();
} while ( outcome == -2 );
displayBoard();
switch (outcome) {
case NOUGHT:
System.out.println("0 wins!");
break;
case CROSS:
System.out.println("X wins!");
break;
case 0:
System.out.println("Tie.");
break;
}
}
}
Some of this was mentioned in the comments, but these conditions fundamentally don't make sense:
if (gameboard [0][0] == NOUGHT && gameboard [0][-1])
return NOUGHT;
} else if (gameboard [0][0] == && CROSS) [0][1] {
return CROSS;
} else if (gameboard [0][0]== && " "()) [0][0] {
return 0;
For example, what do you think that if (gameboard [0][0] == && CROSS) [0][1] is supposed to do? What exactly is " "() supposed to be? And what do you think that == && does? It's difficult to know exactly what you were actually trying to achieve here.
Also, consider gameboard [0][-1]. There are two problems here. First, you do realize that -1 isn't actually a valid array index in Java, right? (That's allowable in Python, but not Java). Also, gameboard [0][-1] is an integer, not a bool, so && gameboard [0][-1] doesn't make sense. If you have something like A && B, both A and B must evaluate to some kind of boolean value (i.e. true or false).
Also, I'd encourage you not to do indentation like you have here. I'd recommend putting each "else if" on its own line.

Nothing returned from 2D arrays for a simple tic-tac-toe board

public String tictactoe(String game)
{
game = game.toUpperCase();
char[][] board = new char[3][3];
int loc = 0;
for( char r = 0; r < board.length; r++ )
{
for( char c = 0; c < board[r].length; c++)
{ board[r][c] = game.charAt(loc);
loc++;
}
}
if ((board[0][0] =='X' && board[0][1] =='X' && board[0][2] =='X') ||
(board[1][0] =='X' && board[1][1] =='X' && board[1][2] =='X') ||
(board[2][0] =='X' && board[2][1] =='X' && board[2][2] =='X'))
return("Player 1 wins horizontally!");
else if ((board[0][0] =='O' && board[0][1] =='O' && board[0][2] =='O') ||
(board[1][0] =='O' && board[1][1] =='O' && board[1][2] =='O') ||
(board[2][0] =='O' && board[2][1] =='O' && board[2][2] =='O'))
return("Player 2 wins horizontally!");
else if ((board[0][0] =='X' && board[1][0] =='X' && board[2][0] =='X') ||
(board[0][1] =='X' && board[1][1] =='X' && board[2][1] =='X') ||
(board[0][2] =='X' && board[1][2] =='X' && board[2][2] =='X'))
return("PLayer 2 wins vertically!");
else if ((board[0][0] =='O' && board[1][0] =='O' && board[2][0] =='O') ||
(board[0][1] =='O' && board[1][1] =='O' && board[2][1] =='O') ||
(board[0][2] =='O' && board[1][2] =='O' && board[2][2] =='O'))
return("Player 2 wins vertically!");
return "Tie!";
}
Above is my code for this method. It reads in a 9 letter string for a tic-tac-toe game and then puts it one by one into a 2D array. I unfortunately have to use this method because this is unfortunately what we're learning, and I've continuously had to bother my teacher about this...
The if statements check each row and column for a winner (I realize it does not check the horizontals). My issue here is nothing is being returned, even though it is supposed to return a string. As I have said I have to use 2D-arrays here. The goal is to check a tic-tac-toe game and return whom as won.
Consider replacing each instance of this
if(board[0][0] =='X' && board[0][1] =='X' && board[0][2] =='X') ||
...
With this function:
public boolean isRowFilledWith(int row_index, char x_or_o) {
return (board[row_idx][0] == x_or_o &&
board[row_idx][1] == x_or_o &&
board[row_idx][2] == x_or_o);
}
Now, to see if the top-most row contains all x-s, call it with
if(isRowFilledWith(0, 'X') ||
...
This will make your code a lot more concise, easier to read and easier to debug.
Try doing:
System.out.println(fooObject.tictactoe("XXXXXXOOO");
inside your main argument (public static void main(String[] args)
I suspect you've confused return and System.out.println. The console will not print anything if returned. Instead, returning is what is thrown at the computer after the function is called. Try printing the result of calling the function...THAT is what is returned to the computer and is only going to be visible if you print it. There may not be any error in your code.
First off, to directly answer your question:
Your function works fine, I locally tested it. I tried both horizontal, and vertical, with both x and o. It must be something you are doing with the caller.
The biggest problem with this code is clarity. You can do quite a bit about clarity by separating the logic into groups.
Clarity can be achieved by making application specific objects.
If you have an object to represent a move, a game, and the marker... you can drastically simplify you code and most of all make it easy to read and debug.
I went ahead and implemented tic tac toe just to kinda explain what I mean by clarity.
Any time you start having massive if else chains and block return statements you may need to take the extra time to break up the problem.
This is fully compililable and I think if you debug through it a few times you will find it is much easier to spot mistakes.
It is also easy to ajust the game... Right now, we have 2 players and a 3 by 3 game.
However, we have extra constructors to adjust the size of the game and to give the gamers some options on what they want thier tic tac toe game to look like.
The major reason to do this is for clarity and debugging.
It is always easier to find issues if you have problem specific methods and problem specific objects.
Below is a fully compilable console application for tic tac toe. In its current form, it plays 100 random 3 by 3 games and prints the results of all 100 games.
The application is also capable of taking your string input as console input and processing the moves.
Notice that we are still using two dimensional arrays, but we hidden away the complexity of using them by encapsulating the array in the Board object.
We also gave a move object so we can store moves to play later. This was useful in creating the loop to play many games, as all the facts about the move are contained in a single object.
Since we created three separate methods for validating the move input. We have a much clearer explanation of what we are validating and why we are doing it.
Most debuggers will also show the objects toString method, so by embedding the game display within the toString() we are allowed to see what the board looks like when we are debugging.
The trick to making clean code that doesn't require alot of comments is to attempt to split out any complexity to its own unit.
You probably will cover more about units of work later, but just keep it in mind, the more simple a method, the more reusable it is. The more single purpose a class is, the better and clearer it is.
My Example:
Mark.java
-This enum is the representation of the mark used by a player to signify their move.
public enum Mark{
X, Y;
}
TicTacToe.java
-This class is your main class
it handles gathering user input, cleaning it, processing it, and displaying the output to the user
Notice that I am not doing game display anywhere else but the main method. this keeps all display issues localized to one spot. The advantage to breaking up your code into specific objects is knowing where to fix something when it breaks.
package com.clinkworks.example;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.clinkworks.example.Move;
import com.clinkworks.example.Board;
import com.clinkworks.example.Mark;
public class TicTacToe {
//this variable is simply used for output. Since we cant save CAT in the mark enum, we
// use it to hold cats games.
private static final String catsGameIdentity = "CAT";
//this map contains the counts of wins for each symbol
//it also keeps track of ties and stores it in the map as "CAT"
//soo.. gamesToWinsMap.get("CAT") will produce the amount of ties.
private static final Map<String, Integer> gamesToWinsMap = new HashMap<String, Integer>();
public static void main(String[] args){
String game = args[0].toUpperCase(); //better ways to do this and no validation here.. but it will work
Board board = new Board();
//holds our current place in the string passed in from teh console
int currentStringLocation = 0;
for(int row = 0; row < 3; row++){ //loop through the rows
for(int column = 0; column < 3; column++){ //for each row loop through the columns
if(gameOver(board)){ // we don't care about the rest of the input if the game was won
break;
}
//convert the symbol to a string for use in teh ValueOf function in the enum class
String symbol = "" + game.charAt(currentStringLocation); //better than String.valueOf imho
//allow spaces to represent un marked places
if(" ".equals(symbol)){
currentStringLocation++; //this accounts for spaces in our input
break;
}
//convert the string to a Mark enum... over use of strings is a very very bad practice
Mark nextMarkToPlace = Mark.valueOf(symbol);
Move move = new Move(row, column, nextMarkToPlace); //we create a move object that encapsulates the complexity of placing the mark on the game board
board.play(move); //the game already knows how to play itself, just let it
currentStringLocation++; //increment the posision.
}
}
//since you may not have won the game, or gave a complete string for a cats game,
// lets at least display the board for debugging reasons.
if(board.movesLeft() > 0){
System.out.println("Board isn't finished, but here is what it looks like basd on your input: ");
System.out.println(board);
}
}
//call me main if you want to see what I do
public static void main2(String[] args) {
//lets play 100 games and see the wins and ties
playGames(100);
System.out.println("Number wins by X: " + gamesToWinsMap.get(Mark.X.name()));
System.out.println("Number wins by O: " + gamesToWinsMap.get(Mark.O.name()));
System.out.println("Number of ties: " + gamesToWinsMap.get(catsGameIdentity));
}
public static void playGames(int count) {
//play a new game each iteration, in our example, count = 100;
for (int i = 0; i < count; i++) {
playGame();
}
}
public static void playGame() {
//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();
//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 < 3; row++) {
for (int col = 0; col < 3; 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("Player with the mark: " + board.whoWon() + " won the game!");
System.out.println(board);
Integer winCount = gamesToWinsMap.get(board.whoWon().name());
winCount = winCount == null ? 1 : winCount + 1;
gamesToWinsMap.put(board.whoWon().name(), winCount);
return true;
} else if (board.movesLeft() == 0) {
System.out.println("It was a cats game!!");
System.out.println(board);
Integer catCount = gamesToWinsMap.get(catsGameIdentity);
catCount = catCount == null ? 1 : catCount + 1;
gamesToWinsMap.put(catsGameIdentity, catCount);
return true;
}
return false;
}
Move.java
-This class is responsible for ensuring that only integers are passed to the game board
and for telling the game board who is doing the move (optional)
package com.clinkworks.example;
import com.clinkworks.example.Mark;
public class Move {
private int row;
private int column;
private Mark forcedMark;
public Move(int row, int column) {
this.row = row;
this.column = column;
}
//the board already knows who should be next. only use this constructor to override
// what symbol to put on the game board
public Move(int row, int column, Mark markToPlace){
this.row = row;
this.column = column;
forcedMark = markToPlace;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
public Mark getMark(){
return forcedMark;
}
}
Board.java
-This is where all the state of the board is managed, who is the next player, who won, and
is there any moves left to play. It also ensures that only valid moves are played.
package com.clinkworks.example;
import com.clinkworks.example.Mark;
import com.clinkworks.example.Move;
public class Board {
private final int rowSize;
private final int columnSize;
private final Mark[][] gameBoard;
private Mark currentMark;
private Mark winningMark;
/**
* This constructor defaults the starting player to X with a 3 by 3
* game.
*/
public Board() {
gameBoard = new Mark[3][3];
currentMark = Mark.X; // X always goes first ;P
winningMark = null;
this.rowSize = 3;
this.columnSize = 3;
}
/**
* This constructor defaults the starting player to X, and lets the
* board size be adjusted
*/
public Board(int rowSize, int columnSize) {
gameBoard = new Mark[rowSize][columnSize];
currentMark = Mark.X; // X always goes first ;P
winningMark = null;
this.rowSize = getRowSize();
this.columnSize = columnSize;
}
/**
* this constructor allows the players to choose who goes first on a 3
* by 3 board.
*
* #param firstPlayer
*/
public Board(Mark firstPlayer) {
gameBoard = new Mark[3][3];
currentMark = firstPlayer; // Let the player choose
winningMark = null;
rowSize = 3;
columnSize = 3;
}
/**
* this constructor allows the players to choose who goes first and to
* choose the size of the board.
*
* #param firstPlayer
*/
public Board(Mark firstPlayer, int rowSize, int columnSize) {
gameBoard = new Mark[getRowSize()][columnSize];
currentMark = firstPlayer; // Let the player choose
winningMark = null;
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 += getMarkAt(x, y) == null ? 1 : 0;
}
}
return moveCount;
}
/**
* If someone won, this will return the winning player.
*
* #return the winning player
*/
public Mark whoWon() {
return winningMark;
}
/**
* This move allows the next player to choose where to place their mark.
* if a move is played without a player given, it will use the current player variable
*
* as a side affect, the next player in rotation is chosen.
*
* #param Move
* #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("Cannot play " + currentMark + " at " + move.getRow() + ", " + move.getColumn() + "\n" + toString());
}
doMove(move);
boolean playerWon = isWinningMove(move);
if (playerWon) {
winningMark = currentMark;
return true;
}
togglePlayer();
boolean outOfMoves = movesLeft() <= 0;
return outOfMoves;
}
public Mark lastMarkPlayed() {
return currentMark;
}
public int getRowSize() {
return rowSize;
}
public int getColumnSize() {
return columnSize;
}
public Mark getCurrentPlayer() {
return currentMark;
}
public Mark getMarkAt(int row, int column) {
return gameBoard[row][column];
}
private void doMove(Move move) {
if(move.getMark() != null){
currentMark = move.getMark();
}
gameBoard[move.getRow()][move.getColumn()] = getCurrentPlayer();
}
private void togglePlayer() {
if (currentMark == Mark.X) {
currentMark = Mark.O;
} else {
currentMark = Mark.X;
}
}
/**
* A valid move is a move where the row and the column are within boundries
* and no move has been made that the location specified by the move.
*/
private boolean validMove(Move move) {
boolean noMarkAtIndex = false;
boolean indexesAreOk = move.getRow() >= 0 || move.getRow() < getRowSize();
indexesAreOk = indexesAreOk && move.getColumn() >= 0 || move.getColumn() < getColumnSize();
if (indexesAreOk) {
noMarkAtIndex = getMarkAt(move.getRow(), move.getColumn()) == null;
}
return indexesAreOk && noMarkAtIndex;
}
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++) {
Mark markOnCol = getMarkAt(move.getRow(), i);
if (markOnCol != getCurrentPlayer()) {
matchesColumn = false;
break;
}
}
return matchesColumn;
}
private boolean winsAcross(Move move) {
boolean matchesRow = true;
for (int i = 0; i < getRowSize(); i++) {
Mark markOnRow = getMarkAt(i, move.getColumn());
if (markOnRow != getCurrentPlayer()) {
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++) {
Mark markOnDiagnal = getMarkAt(i, i);
if (markOnDiagnal != 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();
}
//prints out the board in a nice to view display
public String toString() {
StringBuffer stringBuffer = new StringBuffer();
for(int y = 0; y < getColumnSize(); y++) {
for(int x = 0; x < getRowSize(); x++) {
Mark mark = getMarkAt(x, y);
String markToPrint = "";
if (mark == null) {
markToPrint = " ";
} else {
markToPrint = mark.name();
}
stringBuffer.append("|").append(markToPrint);
}
stringBuffer.append("|\n");
}
return stringBuffer.toString();
}
}
}

Uno Project: Making sure that a illegal plays return correctly?

I am writing a chunk of program to play Uno with other classes. My Uno project is almost finished, but I need to be sure that the part which checks to make sure that on moves after a Wild Card is played, I play either a legal card or return a -1. Here is my code:
import java.util.*;
public class AlexaL_UnoPlayer implements UnoPlayer
{
public int play(List<Card> hand, Card upCard, Color calledColor, GameState state)
{
int play = -1;
boolean haveWild = false;
boolean matchesWildCall = false;
int indexOfWild = 0;
//turn number of cards all players are holding into ints for later use
int[] array = state.getNumCardsInHandsOfUpcomingPlayers();
int playerNext = array[0];
int playerTwoNext = array[1];
int playerBefore = array[2];
Color upCardColor = upCard.getColor();
for(int i = 0; i < hand.size(); i++)
{
//see if I have any wilds
if(hand.get(i).getRank().equals(Rank.WILD) || hand.get(i).getRank().equals (Rank.WILD_D4))
{
haveWild = true;
indexOfWild = i;
}
//set upCard color to calledColor if wild or wild_d4 are played
if (upCard.getRank().equals(Rank.WILD) || upCard.getRank().equals(Rank.WILD_D4))
{
upCardColor = calledColor;
}
//always play a card matching rank of upCard, if possible, or play the first in hand which matches color
if(hand.get(i).getColor().equals(upCardColor))
{
if(hand.get(i).getNumber() == upCard.getNumber())
{
play = i;
}
}
//if cornered(no matching number or color), play a wild
else if(haveWild == true)
{
play = indexOfWild;
}
//hold reverse cards until person next after me has less cards than person before me
if(hand.get(i).getRank().equals(Rank.REVERSE) && playerNext < playerBefore)
{
play = i;
}
//play skips when person next to me has less cards than me
if((hand.get(i).getRank().equals(Rank.SKIP) || hand.get(i).getRank().equals(Rank.DRAW_TWO)) && playerNext < hand.size())
{
play = i;
}
}
return play;
}
public Color callColor(List<Card> hand)
{
//strategy: change the color to the one i'm holding the most of
Color changeTo = Color.GREEN;
int numBlues = 0;
int numGreens = 0;
int numReds = 0;
int numYellows = 0;
//find out how many of each color i'm holding
for(int i = 0; i < hand.size(); i++)
{
if(hand.get(i).getColor().equals(Color.BLUE))
{
numBlues++;
}
else if(hand.get(i).getColor().equals(Color.RED))
{
numReds++;
}
else if(hand.get(i).getColor().equals(Color.GREEN))
{
numGreens++;
}
else if(hand.get(i).getColor().equals(Color.YELLOW))
{
numYellows++;
}
}
//find out which i'm holding the most of and call that color
//if no majority, return my favorite color(green)
if(numBlues > numReds && numBlues > numGreens && numBlues > numYellows)
{
changeTo = Color.BLUE;
}
else if(numReds > numBlues && numReds > numGreens && numReds > numYellows)
{
changeTo = Color.RED;
}
else if(numGreens > numBlues && numGreens > numYellows && numGreens > numReds)
{
changeTo = Color.GREEN;
}
else if(numYellows > numBlues && numYellows > numGreens && numYellows > numReds)
{
changeTo = Color.YELLOW;
}
else
{
changeTo = Color.GREEN;
}
return changeTo;
}
}
For some reason, my output is telling me this:
You were given this hand:
0. G7
1. G5
2. G+2
and the up card was: W
and the called color was: YELLOW
and you (wrongly) returned 2.
Valid plays would have included: -1
Can anyone provide some insight on why I am getting this error and how to fix it? Much appreciated!
Based on just this code, it is hard to say what is wrong with your implementation.
Did you try step-by-step debugging of your play function?
Alternately, it would be beneficial to instrument your code with more trace statements to better figure out where the issue might be occurring.
For example:
Every where your play variable gets assigned a value, print its value to console with some context of where you are in code.
if(hand.get(i).getNumber() == upCard.getNumber())
{
play = i;
System.out.println("number match step: play = " + play ); // You can add such a line
}
This should give you an idea as to when play's value is getting mutated to 2 unexpectedly.
Just clutching at straws here, but what are the expected values of getColor and getNumber for wild cards.
In case you have them set to Green or 2 respectively, it might explain the output you are seeing because of the following fragment of code.
//always play a card matching rank of upCard, if possible, or play the first in hand which matches color
if(hand.get(i).getNumber() == upCard.getNumber())
{
play = i;
}
else if(hand.get(i).getColor() == upCardColor)
{
play = i;
}

a method always returns false even if i enter valid values

in this method i have tried to set row =0 , row =1 row =2 row =3 row =4 and the result is always false. i think its something considering the initilization of the variable valid but if i do not initialize the compiler comes up with error
public static boolean isValidMove(int row,int col){
boolean valid=false;
if(row>=0&&row<3&&col>=0&&col<3){
if (takenSquare[row][col]==false) {
valid= true;
}
}
Seems like your condition is never evaluated at all
public static boolean isValidMove( int nRow,int nCol ) {
boolean blValid = false;
if( ( nRow >= 0 && nRow < 3 ) && ( nCol >= 0 && nCol < 3 ) ) {
if ( arTakenSquare[ nRow ][ nCol ] == false ) {
blValid = true;
}
}
return blValid;
}
May be this would help.
Your code is valid (for values from 0 to 2). Of course you need a return statment. The only thing that can be wrong is takenSquare[row][col]==false. Is that a array of boolean values?
This is not an answer, but an extension to my comment suggesting an SSCCE. I have taken the code from the question, and wrapped it in a simple test program. To complete the isValidMove method I added a return of valid and a }.
It prints row=0, col=0: true. This confirms the bug is not in the code quoted in the question.
The next step is for the OP to either modify this until it reproduces the problem, or simplify the rest of the OP's program until it is about this long. Either way, the objective is a short, simple program that compiles, runs, and demonstrates the problem. Often, the attempt write such a thing will bring out the bug. If it does not, posting the SSCCE will certainly get a useful answer.
public class Test
{
private static boolean[][] takenSquare = new boolean[3][3];
public static void main(String[] args) {
System.out.println("row=0, col=0: "+isValidMove(0,0));
}
public static boolean isValidMove(int row, int col) {
boolean valid = false;
if (row >= 0 && row < 3 && col >= 0 && col < 3) {
if (takenSquare[row][col] == false) {
valid = true;
}
}
return valid;
}
}
================================================================
Here's a revised version of my test program that demonstrates instrumenting isValidMove so that it is clear what arguments it was passed, what it is returning, and why it is returning false any time it does so.
public class Test
{
private static boolean[][] takenSquare = new boolean[3][3];
public static void main(String[] args) {
takenSquare[1][1] = true;
test(0,0);
test(0,-1);
test(1,1);
}
private static void test(int row, int col){
System.out.println("Test result: row="+row+", col="+col+", valid="+isValidMove(row, col));
}
public static boolean isValidMove(int row, int col) {
boolean valid = false;
if (row >= 0 && row < 3 && col >= 0 && col < 3) {
if (takenSquare[row][col] == false) {
valid = true;
} else {
System.out.println("isValidMove square already taken for row="+row+", col="+col);
}
} else {
System.out.println("isValidMove out of range argument row="+row+" col="+col);
}
System.out.println("Returning "+valid+" from isValidMove("+row+","+col+")");
return valid;
}
}

Categories