How to dance around a NullPointerException? - java

I'm trying to check whether or not a move is legal in the game Othello, using eclipse and gridworld. The first thing I do to the location is check if it is valid, but in order to check the validity location, it needs to not be null. The problem is, one of the requirements of it being a legal move is that it is null/empty/unoccupied. How do I avoid this? I have pointed out where the error is supposedly at. (Sorry if this confused anyone.)
public boolean isLegal(Location loc1)
{
boolean isLegal = false;
String currentColor = currentPlayer.getColor();
int row = loc1.getRow();
int col = loc1.getCol();
if(board.isValid(loc1))
{
if(board.get(loc1) == null)
{
for(Location tempLoc : board.getValidAdjacentLocations(loc1))
{
**if(!board.get(tempLoc).equals(currentColor))**
{
if((row != tempLoc.getRow()) && (col == tempLoc.getCol()))
{
//count up column
if(tempLoc.getRow() < row)
{
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-2, col);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down column
else
{
for(int i = row; i < 6;)
{
Location tempLoc2 = new Location(i+2, col);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
}
else
{
i=9;
isLegal = true;
}
}
}
}
else if(col != tempLoc.getCol() && row == tempLoc.getRow())
{
//count right row
if(col > tempLoc.getCol())
{
for(int i = col; i > 1;)
{
Location tempLoc2 = new Location(row, i-2);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count left row
else
{
for(int i = col; i < 6;)
{
Location tempLoc2 = new Location(row, i+2);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
}
else
{
i=9;
isLegal = true;
}
}
}
}
else
{ //count up/right diag
if(row-1 == tempLoc.getRow() && col+1 == tempLoc.getCol())
{
int j = col;
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-1, j+1);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
j++;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down/left diag
else if(row+1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
{
int i = row;
for(int j = col; j > 1;)
{
Location tempLoc2 = new Location(i+1, j-1);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
j--;
}
else
{
i=9;
isLegal = true;
}
}
}
//count up/left diag
else if(row-1 == tempLoc.getRow() && col-1 == tempLoc.getCol())
{
int j = col;
for(int i = row; i > 1;)
{
Location tempLoc2 = new Location(i-1, j-1);
if(!board.get(tempLoc2).equals(currentColor))
{
i--;
j--;
}
else
{
i=-1;
isLegal = true;
}
}
}
//count down/right diag
else
{
int j = col;
for(int i = row; i > 6;)
{
Location tempLoc2 = new Location(i+1, j+1);
if(!board.get(tempLoc2).equals(currentColor))
{
i++;
j++;
}
else
{
i=-1;
isLegal = true;
}
}
}
}
}
}
}
}
return isLegal;
}

One solution is to change your design so that no location is ever null.
You seem to have equated null with "unoccupied" or "empty". Instead create all positions first (there aren't many of them on an Othello board) and initialize them all with boolean occupied = false or an equivalent member variable. Then you'd have:
if ( !board.get(loc1).isOccupied() ) { /*stuff*/ }
instead of a null check.
This is better object oriented design because an empty location is still a location, and should be manipulable.

You shouldn't use null as a part of your logic.
null it's not a state, it is a symbol that there is no state.
You should leave null out of your logic, then if some reference is null, you know that for sure something really nasty happen, not related to your model. Inside Location you can create for example a method isEmpty() or similar, so you can easily avoid comparing to null.

Use an enum with the values BLACK, WHITE and VACANT, instead of a String, to store what colour token is at each location. Return the same enum from getColor() in the Player class.

Related

Peg Solitaire can't always find a path

I develope a software that should solve some types of Peg Solitaire and print the steps to solve it like that:
[3,3,"^"],[4,5,"<"]
I'm using a Backtracking algorithm and most of times(not always) it's work on the "original" board, the france one and another one that is 5x6, but when i try to solve a new one that look like that:
XXXXXXXXX
XXXXXXXXX
XX     XX
XX     XX
XXXXXXXXX
XXXXXXXXX
the program can't find the answer, I try some basic board and this work but after 3 or 4 steps the program can't find the path.
I Try to debbug the code but can't find why he can't find the path
my PegSolver:
package com.company;
public class PegSolver {
public int peg=0;
String answer[];
public boolean superLevel=false;
public void start(){
boolean makefrance=false;
boolean lastLevel = false;
//france
String end,start;
//the starting board
start="OOOOOOOOO\", \"OOOOOOOOO\", \"OO OO\", \"OO O.\", \"OO OO\", \"OOOOOOOOO\", \"OOOOOOOOO\"";
//the wanted board
end = " OOOOOOOOO\", \"OOOOOOOO.\", \"OO OO\", \"OO O.\", \"OO OO\", \"OOOOOOOO.\", \"OOOOOOOO.\"] ";
//clean the Strings, let only 'O' or '.'
start = removeUnwanted(start);
end = removeUnwanted(end);
//adapt the board to normal, france, or other by the number of char in the string after remove the unwanted chars
if(start.length()==33)
{
makefrance=false;
lastLevel=false;
}
else if(start.length()==37)
{
makefrance = true;
lastLevel = false;
}
else if(start.length()==30){
makefrance=false;
lastLevel=true;
}
else if(start.length()==48){
superLevel=true;
}
int startDots=0;
int endDots=0;
Board board;
Board destinationBoard;
if(lastLevel)
{
board = new Board(start,true,true,superLevel);//The start of my board
destinationBoard = new Board(end,true,true,superLevel);//the wanted destination board, '.' for no peg and O for peg.
}
else{
board = new Board(start,makefrance,lastLevel,superLevel);//The start of my board
destinationBoard = new Board(end,makefrance,lastLevel,superLevel);//the wanted destination board, '.' for no peg and O for peg.
}
//count the pegs from the begining and ending board
for(int i=0;i<start.length()-1;i++) {
if (start.charAt(i) == '.') {
startDots++;
}
}
for(int i=0;i<end.length()-1;i++) {
if (end.charAt(i) == '.') {
endDots++;
}
}
//calculate the number of turns to the destination, every turn only one peg is disapear so finally there is:
//pegs at the end - pegs at the begining = number of turns
peg=endDots-startDots;
//create a string for save the steps.
answer = new String[peg];
//if we have a result, return the answer and print a text
if(Solve(board,destinationBoard,0))
{
System.out.println("The best day of my life");
for(int i=0;i<answer.length;i++)
{
System.out.println(answer[i]);
}
}
//if don't find path, print WTF
else{
System.out.println("WTFFFFF");
}
}
//recurse method that solve the peg solitaire
public boolean Solve(Board board, Board destination, int turn)
{
int rows=0;
//if we are on the end of the game
if(turn==peg)
{
//check if the current board is like the destination board.
if(board.isFinished(destination))
{
System.out.println("found the path");
return true;//this board is the same as the desitnation
}
else
{
return false;//this board is not the same as the desitnation
}
}
else {
if (superLevel)//if its a bigger board, need to check the board on more rows and lines
{
rows=9;
}
else{
rows=7;
}
for(int i=0;i<rows;i++){
for(int j=0;j<rows;j++)
{
if(board.isLegal(j,i))
{
if(board.canTurn(j,i,"right"))//test if it's possible to turn right
{
if(Solve(board.turn(board,j,i,"right"),destination,turn+1))//create a new board after changment of direction and recurse
{
answer[turn]= "["+j+","+i+",\">\"],";//add this step to the answer
return true;
}
}
if(board.canTurn(j,i,"up"))//test if it's possible to turn up
{
if(Solve(board.turn(board,j,i,"up"),destination,turn+1))//create a new board after changment of direction and recurse
{
answer[turn]= "["+j+","+i+",\"^\"],";//add this step to the answer
return true;
}
}
if(board.canTurn(j,i,"down"))//test if it's possible to turn down
{
if(Solve(board.turn(board,j,i,"down"),destination,turn+1))//create a new board after changment of direction and recurse
{
answer[turn]= "["+j+","+i+",\"v\"],";//add this step to the answer
return true;
}
}
if(board.canTurn(j,i,"left"))//test if it's possible to turn left
{
if(Solve(board.turn(board,j,i,"left"),destination,turn+1))//create a new board after changment of direction and recurse
{
answer[turn]= "["+j+","+i+",\"<\"],";//add this step to the answer
return true;
}
}
}
}
}
return false;//if don't find any path return.
}
}
public String removeUnwanted(String boardText)
{
String returnBoard="";
for(int i=0; i<boardText.length();i++)
{
if(boardText.charAt(i) == '.' || boardText.charAt(i)== 'O')//remove all the char that are not '.' or 'O'
{
returnBoard += boardText.charAt(i);
}
}
System.out.println(returnBoard);
return returnBoard;
}
}
and my board class:
package com.company;
import java.security.PublicKey;
public class Board {
//the array that storage the board data
private int[][] board = new int[9][9];;
public boolean superLvl=false;
int pegs = 0;
public Board(String place, boolean makefrance,boolean lastLevel,boolean superlevel) {
int boardx,boardy;
//add 2 to make the board compatible to the string
if(superlevel) {
superLvl=true;
boardx = 9;
boardy = 9;
int[][] temp = new int[][]{{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,2,2,2,2,2,0,0},{0,0,2,2,2,2,2,0,0},{0,0,2,2,2,2,2,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{2,2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2,2}};
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
board[j][i]=temp[i][j];
}
}
}
else {
if (!lastLevel) {
boardx = 7;
boardy = 7;
board[0][0] = 2;
board[1][0] = 2;
board[5][0] = 2;
board[6][0] = 2;
board[0][1] = 2;
board[1][1] = 2;
board[5][1] = 2;
board[6][1] = 2;
board[0][5] = 2;
board[1][5] = 2;
board[5][5] = 2;
board[6][5] = 2;
board[0][6] = 2;
board[1][6] = 2;
board[5][6] = 2;
board[6][6] = 2;
if (makefrance) {
board[1][1] = 1;
board[1][5] = 1;
board[5][1] = 1;
board[5][5] = 1;
}
} else {
boardx = 6;
boardy = 6;
board[0][6] = 2;
board[1][6] = 2;
board[2][6] = 2;
board[3][6] = 2;
board[4][6] = 2;
board[5][6] = 2;
board[6][6] = 2;
board[0][5] = 2;
board[1][5] = 2;
board[2][5] = 2;
board[3][5] = 2;
board[4][5] = 2;
board[5][5] = 2;
board[6][5] = 2;
board[6][0] = 2;
board[6][1] = 2;
board[6][2] = 2;
board[6][3] = 2;
board[6][4] = 2;
}
}
int loca = 0;//location on the string of place
//add 0 or 1 to the table
for (int i = 0; i < boardy; i++) {
for (int j = 0; j < boardx; j++) {
if (board[j][i] != 2) {
if (place.charAt(loca) == 'O') {
loca++;
board[j][i] = 1;
pegs++;
} else if (place.charAt(loca) == '.') {
loca++;
board[j][i] = 0;
}
}
}
}
System.out.println();
this.printBoard();
}
public Board(Board newone)
{
int placement=7;
if(superLvl)
{
placement=9;
}
for(int i=0;i<placement;i++)
{
for(int j=0;j<placement;j++)
{
board[i][j]= newone.getValue(i,j);
}
}
}
//test if this board is like the other board at the end of the game
public boolean isFinished(Board destination) {
int placement=7;
if(superLvl)
{
placement=9;
}
for (int i = 0; i < placement; i++) {
for (int j = 0; j < placement; j++) {
if (this.getValue(j,i)!=destination.getValue(j,i))
{
return false;
}
}
}
return true;
}
//get the value of this coordinate.
public int getValue(int x, int y)
{
return board[x][y];
}
//test if this cooardiante is a place on the board(not 2)
public boolean isLegal(int x,int y)
{
if(board[x][y]!=2)
{
return true;
}
else{
return false;
}
}
//test if can turn- if this place is 1 and the next is 1 and the second next is 0
//also protect from stack overflow
public boolean canTurn(int x,int y,String direction)
{
if(direction.equals("right"))
{
if(x<5){
if(this.getValue(x,y)==1 && this.getValue(x+1,y)==1 && this.getValue(x+2,y)==0)
{
return true;
}
else {
return false;
}
}
else
{
return false;
}
}
else if(direction.equals("left"))
{
if(x>1){
if(this.getValue(x,y)==1 && this.getValue(x-1,y)==1 && this.getValue(x-2,y)==0)
{
return true;
}
else {
return false;
}
}
else
{
return false;
}
}
else if(direction.equals("up"))
{
if(y>1){
if(this.getValue(x,y)==1 && this.getValue(x,y-1)==1 && this.getValue(x,y-2)==0)
{
return true;
}
else {
return false;
}
}
else
{
return false;
}
}
else if(direction.equals("down"))
{
if(y<5){
if(this.getValue(x,y)==1 && this.getValue(x,y+1)==1 && this.getValue(x,y+2)==0)
{
return true;
}
else {
return false;
}
}
else
{
return false;
}
}
else{
System.out.println("method canTurn on board get a wrong direction string");
return false;
}
}
//make the move and return a "new" board for saving the originial one.
public Board turn(Board oldBoard,int x, int y ,String direction)
{
Board board = new Board(oldBoard);
if(direction.equals("right"))
{
board.setValue(x,y,0);
board.setValue(x+1,y,0);
board.setValue(x+2,y,1);
return board;
}
else if(direction.equals("left"))
{
board.setValue(x,y,0);
board.setValue(x-1,y,0);
board.setValue(x-2,y,1);
return board;
}
else if(direction.equals("up"))
{
board.setValue(x,y,0);
board.setValue(x,y-1,0);
board.setValue(x,y-2,1);
return board;
}
else if(direction.equals("down"))
{
board.setValue(x,y,0);
board.setValue(x,y+1,0);
board.setValue(x,y+2,1);
return board;
}
else{
System.out.println("method Turn on board get a wrong direction string");
return null;
}
}
//change the value of the wanted board.
public void setValue(int x,int y,int value)
{
board[x][y]=value;
}
//print the board, made for tests.
public void printBoard()
{
int placement=7;
if(superLvl)
{
placement=9;
}
for(int i=0;i<placement;i++)
{
for(int j=0;j<placement;j++)
{
System.err.print(board[j][i]);
}
System.err.println();
}
System.err.println("------------------");
}
}

tic tac toe java user vs computer

I am writing a tic tac toe game for my class. Everything is working but I am unable to figure out how to make my computer player choose only spaces that are available. My code is glitching and allowing the computer to choose either the other players spaces or not playing at all. Any help will be appreciated.
import java.util.Random;
import java.util.Scanner;
public class TicTacToe1 {
public static void main(String[] args) {
welcome();
initializeBoard();
printBoard();
while ((!checkWin()) && (!checkDraw())) {
playerMove();
printBoard();
System.out.println();
computerMove();
printBoard();
}
System.out.println();
if (checkWin() == true) {
System.out.println("The winner is " + currentTurn);
}
if (checkDraw() == true) {
System.out.println("Draw");
}
}
private static String[][] board = new String[3][3];
private static int row, column;
public static Scanner scan = new Scanner(System.in);
public static String currentTurn = "X";
// public static String computerTurn = "O";
public static String turn() {
if (currentTurn == "X") {
currentTurn = "O";
} else {
currentTurn = "X";
}
return currentTurn;
}
private static void welcome() {
System.out.println("Tic Tac Toe");
System.out.println("Please enter your coordinates for your location row (1-3) column (1-3):");
}
public static void initializeBoard() { // initialize tic tac toe
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j] = "-";
}
}
}
public static void printBoard() {
for (int i = 0; i < board.length; i++) {
System.out.println();
for (int j = 0; j < board.length; j++) {
if (j == 0) {
System.out.print("| ");
}
System.out.print(board[i][j] + " | ");
}
}
}
public static void playerMove() {
System.out.println();
System.out.println("Your Move: ");
row = scan.nextInt() - 1;
column = scan.nextInt() - 1;
if (board[row][column] == "-") {
board[row][column] = turn();
} else {
System.out.println("Invalid entry. Please go again");
row = scan.nextInt() - 1;
column = scan.nextInt() - 1;
board[row][column] = turn();
}
}
// public static void computerMove() {
// Random computerMove = new Random();
// row = computerMove.nextInt(3);
// column = computerMove.nextInt(3);
// if (board[row][column] == "-") {
// board[row][column] = turn();
// } else {
// }
// }
public static void computerMove() {
Random computerMove = new Random();
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
while (board[row][column] != "-") {
// Random computerMove = new Random();
// row = computerMove.nextInt(3);
// column = computerMove.nextInt(3);
if (board[row][column] == "-") {
board[row][column] = turn();
} else {
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
board[row][column] = turn();
}
}
}
public static boolean checkWin() {
return (checkDiagonalWin() || checkHorizontalWin() || checkVerticalWin());
}
public static boolean checkDiagonalWin() {
if ((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) && (board[1][1] != "-")) {
return true;
}
if ((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) && (board[1][1] != "-")) {
return true;
}
return false;
}
public static boolean checkHorizontalWin() {
// for (int i = 0; i < board.length; i++) {
if ((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]) && (board[0][0] != "-")) {
return true;
}
if ((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]) && (board[1][0] != "-")) {
return true;
}
if ((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]) && (board[2][0] != "-")) {
return true;
}
// }
return false;
}
public static boolean checkVerticalWin() {
// for (int j = 0; j < board.length; j++) {
if ((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]) && (board[0][0] != "-")) {
return true;
}
if ((board[0][1] == board[1][1]) && (board[0][1] == board[2][1]) && (board[0][1] != "-")) {
return true;
}
if ((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]) && (board[0][2] != "-")) {
return true;
}
// }
return false;
}
public static boolean checkDraw() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j] == "-") {
return false;
}
}
}
return true;
}
}
The issue was in your computerMove logic.
public static void computerMove() {
Random computerMove = new Random();
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
while (board[row][column] != "-") {
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
}
board[row][column] = turn();
}
This should work for you, just copy paste this in place of your computerMove.
Now as to why your code didn't work:-
Your code:
while (board[row][column] != "-") {
if (board[row][column] == "-") {
board[row][column] = turn();
} else {
row = computerMove.nextInt(3);
column = computerMove.nextInt(3);
board[row][column] = turn();
}
}
The while loop looks at the position and sees that there is no '-', thus runs. Then inside your while loop you have a if statement which checks to see whether you have '-' at that position. That can never be true, because our while loop wouldn't run otherwise.
The best idea is to let your code keep changing the row and columns until you get a position with '-', and use your while loop to do that. As soon as you get the '-', your while loop won't run anymore anyways, so you can just set the board[row][columns] = turn() just outside the while loop, and your code will work fine.
P.S. Took a lot of willpower to not make a machines are uprising reference to your
My code is glitching and allowing the computer to choose either the other players spaces or not playing at all
Have fun with your program :)
~HelpfulStackoverflowCommunity

Problem in toString function in java (works with local variable)

i have a class in a java program where i am using a toString function to retrieve data. the toString checks a private function in the same class which returns a int value, for displaying different types of return messages.~
The problem is that if i use a local variable in the string function every turns out good, but if i check in the if statements directlly the private function, this function doesnt return any value.
private int computerTryHorizontalPlay() {
int repeatedMyValueCount = 0;
int repeatedYourValueCount = 0;
int[] myPositions = new int[3];
int[] yourPositions = new int[3];
for (int a = 0; a < 3; a++) {
int repeatedMyValue = 0;
int repeatedYourValue = 0;
int emptyFields = 0;
int[] emptyPosition = new int[2];
for (int b = 0; b < 3; b++) {
if (jogoGalo[a][b] == 'X') {
repeatedMyValue++;
} else if (jogoGalo[a][b] == 'O') {
repeatedYourValue++;
}
if (jogoGalo[a][b] == '-') {
emptyPosition[0] = a;
emptyPosition[1] = b;
emptyFields++;
}
}
if (repeatedMyValue == 3 || repeatedYourValue == 3) {
return 3;
} else {
if (emptyFields == 1) {
if (repeatedMyValue == 2) {
repeatedMyValueCount++;
myPositions[repeatedMyValueCount - 1] = emptyPosition[0];
myPositions[repeatedMyValueCount] = emptyPosition[1];
} else if (repeatedYourValue == 2) {
repeatedYourValueCount++;
yourPositions[repeatedYourValueCount - 1] = emptyPosition[0];
yourPositions[repeatedYourValueCount] = emptyPosition[1];
}
}
}
}
if (repeatedMyValueCount > 0) {
jogoGalo[myPositions[0]][myPositions[1]] = 'X';
return 2;
} else if (repeatedYourValueCount > 0) {
jogoGalo[yourPositions[0]][yourPositions[1]] = 'X';
return 1;
}
return 0;
}
This doesn´t work!
public String toString() {
if(computerTryHorizontalPlay() == 3) {
return "The game has already ended!";
}
else if(computerTryHorizontalPlay() == 2) {
return "Computer won!";
}
else if(computerTryHorizontalPlay() == 1) {
return "Computer defendeu!";
}
return null;
}
This works!
public String toString() {
int horizontalFunctionValue = computerTryHorizontalPlay();
if(horizontalFunctionValue == 3) {
return "The game has already ended!";
}
else if(horizontalFunctionValue == 2) {
return "Computer won!";
}
else if(horizontalFunctionValue == 1) {
return "Computer defendeu!";
}
return null;
}
}
toString() must be a read-only method, i.e. it is not allowed to have side-effects like changing the state of the object. Since computerTryHorizontalPlay() is a state-changing method, you are not allowed to call it from toString().
Since the only state-change happens in the last if statement, you can change the code to not execute the play when called from toString(), like this:
private int computerTryHorizontalPlay() {
return computerTryHorizontalPlay(true);
}
private int computerTryHorizontalPlay(boolean doMove) {
// lots of code here
if (repeatedMyValueCount > 0) {
if (doMove)
jogoGalo[myPositions[0]][myPositions[1]] = 'X';
return 2;
} else if (repeatedYourValueCount > 0) {
if (doMove)
jogoGalo[yourPositions[0]][yourPositions[1]] = 'X';
return 1;
}
return 0;
}
public String toString() {
if(computerTryHorizontalPlay(false) == 3) {
return "The game has already ended!";
}
else if(computerTryHorizontalPlay(false) == 2) {
return "Computer won!";
}
else if(computerTryHorizontalPlay(false) == 1) {
return "Computer defeated!";
}
return null;
}

Java connect four Diagonal win error and error when dropping coin in column 6

I am new to java and have an assignment which asks me to make a connect4 game. Everything in my code is working perfectly fine, other than the diagonals and for some reason I cannot enter a coin in column 6. Also, if any other error is found please point that out as well! Any help will be appreciated. Thanks!
public class Connect4 {
/**
* #param args the command line arguments
*/
public static void displayGrid(char [][]connectfourGrid){
for(int i=0;i<connectfourGrid.length;i++)
{
for(int j=0;j<connectfourGrid[i].length;j++)
System.out.print("|"+connectfourGrid[i][j]);
System.out.println("|");
}
for(int i=0;i<connectfourGrid.length-1;i++)
System.out.print("---");
System.out.println("");
}
public static void playConnect4(char[][]connectfourGrid){
Scanner input = new Scanner(System.in);
boolean gameOver=false;
boolean playersTurn = true;
int columnPos = 0;
char colour;
while(!gameOver){
if(playersTurn){
System.out.print("Drop a red coin at column 0-6:");
colour = 'R';
}
else{
System.out.print("Drop a yellow coin at column 0-6:");
colour = 'Y';
}
columnPos = input.nextByte();
while(columnPos<0||columnPos>6){
System.out.println("C'est out of bounds. "+colour+" please enter again mate");
columnPos=input.nextByte();
}
playersTurn = !playersTurn;
if(dropCoin(connectfourGrid, columnPos, colour))
playersTurn=!playersTurn;
else{
displayGrid(connectfourGrid);
if(gameStatus(connectfourGrid,columnPos,colour)){
gameOver=true;
System.out.println(colour+" won!");
}
else if(checkTie(connectfourGrid)){
gameOver = true;
System.out.println("C'est a tie!");
}
}
}
}
public static boolean checkTie(char[][]connectfourGrid){
for(int i=0;i<connectfourGrid[0].length;i++)
if(connectfourGrid[0][i]==0)
return false;
return true;
}
public static boolean gameStatus(char[][]connectfourGrid,int columnPos,char colour){
int rowPos=0;
for(int i=0;i<connectfourGrid.length;i++)
if(connectfourGrid[i][columnPos]!=0){
rowPos = i;
break;
}
if(checkCol(connectfourGrid,columnPos,colour,rowPos))
return true;
if(checkRow(connectfourGrid,columnPos,colour,rowPos))
return true;
if(checkMajorDiagonal(connectfourGrid,columnPos,colour,rowPos))
return true;
if(checkMinorDiagonal(connectfourGrid,columnPos,colour,rowPos))
return true;
return false;
}
public static boolean checkMajorDiagonal(char[][]connectfourGrid,int columnPos,char colour,int rowPos){
int count=1;
for(int i=rowPos-1,j=columnPos-1;i>=0&&j>=0;i--,j--)
if(colour==connectfourGrid[i][j])
return true;
else
break;
if(count>=4)
return true;
for(int i=rowPos+1,j=columnPos+1;i<connectfourGrid.length&&j<connectfourGrid[0].length;i++,j++)
if(colour==connectfourGrid[i][j])
return true;
else
break;
if(count>=4)
return true;
/* for(int i=rowPos-1;i>=0;i--)
for(int j=columnPos-1;j>=0;j--)
if(colour==connectfourGrid[i][j])
count++;
else
break;
if(count>=4)
return true;
for(int i=rowPos+1;i<connectfourGrid.length;i++)
for(int j=columnPos+1;j<connectfourGrid[0].length;j++)
if(colour==connectfourGrid[i][j])
return true;
else
break;
if(count>=4)
return true;*/
return false;
}
public static boolean checkMinorDiagonal(char[][]connectfourGrid,int columnPos,char colour,int rowPos){
int count=1;
for(int i=rowPos+1,j=columnPos-1;i<connectfourGrid.length&&j>=0;i++,j--)
if(colour==connectfourGrid[i][j])
count++;
else
break;
if(count>=4)
return true;
for(int i=rowPos-1,j=columnPos+1;i>=0&&j<connectfourGrid[0].length;i--,j++)
if(colour==connectfourGrid[i][j])
count++;
else
break;
if(count>=4)
return true;
/*for(int i=rowPos+1;i<connectfourGrid.length;i++)
for(int j=columnPos-1;j>=0;j--)
if(colour==connectfourGrid[i][j])
count++;
else
break;
if(count>=4)
return true;
for(int i=rowPos-1;i>=0;i--)
for(int j=columnPos+1;i<connectfourGrid[0].length;j++)
if(colour==connectfourGrid[i][j])
count++;
else
break;
if(count>=4)
return true;*/
return false;
}
public static boolean checkRow(char[][]connectfourGrid,int columnPos,char colour,int rowPos){
int count=1;
for(int i=columnPos-1;i>=0;i--)
if(colour==connectfourGrid[rowPos][i])
count++;
else
break;
if(count>=4)
return true;
for(int i=columnPos+1;i<connectfourGrid[0].length;i++)
if(colour==connectfourGrid[rowPos][i])
count++;
else
break;
if(count>=4)
return true;
return false;
}
public static boolean checkCol(char [][]connectfourGrid,int columnPos,char colour,int rowPos){
int count=1;
if((rowPos+4)<=6)
for(int i=rowPos+1;i<=(rowPos+3);i++)
if(colour==connectfourGrid[i][columnPos])
count++;
else
break;
if(count==4)
return true;
return false;
}
public static boolean dropCoin(char[][]connectfourGrid, int columnPos,char colour){
for(int i=connectfourGrid.length-1;i>=0;i--)
if(connectfourGrid[i][columnPos]==0){
connectfourGrid[i][columnPos]=colour;
return false;
}
System.out.println("C'est full. "+colour+" try again mate!");
return true;
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
char[][]connectfourGrid = new char [6][7];
displayGrid(connectfourGrid);
playConnect4(connectfourGrid);
}
}
This doesn't seem to make sense...
public static boolean checkMajorDiagonal(char[][] connectfourGrid, int columnPos, char colour, int rowPos) {
int count = 1;
System.out.println(columnPos + "x" + rowPos);
for (int i = rowPos - 1, j = columnPos - 1; i >= 0 && j >= 0; i--, j--) {
if (colour == connectfourGrid[i][j]) {
return true;
} else {
break;
}
}
if (count >= 4) {
return true;
}
for (int i = rowPos + 1, j = columnPos + 1; i < connectfourGrid.length && j < connectfourGrid[0].length; i++, j++) {
if (colour == connectfourGrid[i][j]) {
return true;
} else {
break;
}
}
if (count >= 4) {
return true;
}
return false;
}
The moment you find a cell with a matching color you return true, which doesn't take into account if there are other matching coins, which would be required in order to form a winning condition.
What's even weirder is you've got code setup to check the number of "matches", but aren't using them.
So, when I changed it to...
public static boolean checkMajorDiagonal(char[][] connectfourGrid, int columnPos, char colour, int rowPos) {
int count = 1;
System.out.println(columnPos + "x" + rowPos);
for (int i = rowPos - 1, j = columnPos - 1; i >= 0 && j >= 0; i--, j--) {
if (colour == connectfourGrid[i][j]) {
//return true;
count++;
} else {
break;
}
}
if (count >= 4) {
return true;
}
for (int i = rowPos + 1, j = columnPos + 1; i < connectfourGrid.length && j < connectfourGrid[0].length; i++, j++) {
if (colour == connectfourGrid[i][j]) {
//return true;
count++;
} else {
break;
}
}
if (count >= 4) {
return true;
}
return false;
}
I got it to work (in my limited testing).
What's additional weird, you seem to do it correctly for checkMinorDiagonal.
I also had no issue getting coins in to column 6

need help on my tic tac toe demo invalid top level statement

public class TicTacToe
{
private char currentPlayer;
private char[][] board;
public TicTacToe()
{
board = new char [3][3];
currentPlayer = 'x';
startBoard();
}
public void startBoard()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = '-';
}
}
}
public void makeBoard()
{
System.out.println("---------------");
for (int i = 0; i < 3; i++)
{
System.out.print("| ");
for (int j = 0; j < 3; j++)
{
System.out.print(board[i][j] + " | ");
}
System.out.println();
System.out.println("---------------");
}
}
public boolean fullBoard()
{
boolean full = true;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] == '-')
{
full = false;
}
}
}
return full;
}
public boolean win()
{
return (rowWin() || columnWin() || diagWin());
}
private boolean rowWin()
{
for (int i = 0; i < 3; i++)
{
if (rowColumn(board[i][0], board[i][1], board[i][2]) == true)
{
return true;
}
}
return false;
}
private boolean columnWin()
{
for (int i = 0; i < 3; i++)
{
if (rowColumn(board[0][i], board[1][i], board[2][i]) == true)
{
return true;
}
}
return false;
}
private boolean diagWin()
{
return ((rowColumn(board[0][0], board[1][1], board[2][2]) == true) ||
(rowColumn(board[0][2], board[1][1], board[2][0]) == true));
}
private boolean rowColumn(char rc1, char rc2, char rc3)
{
return ((rc1 != '-') && (rc1 == rc2) && (rc2 == rc3));
}
public void playerChange()
{
if (currentPlayer == 'x')
{
currentPlayer = 'o';
}
else
{
currentPlayer = 'x';
}
}
public boolean placeMark(int row, int column)
{
if ((row >= 0) && (row < 3))
{
if ((column >= 0) && (column < 3))
{
if (board[row][column] == '-')
{
board[row][column] = currentPlayer;
return true;
}
}
}
return false;
}
}
public class TicTacToedemo
{
public static void main(String[] args)
{
TicTacToe demo = new TicTacToe();
demo.makeBoard();
if (demo.win())
System.out.println("Winner! Hooray!");
else if (demo.fullBoard())
System.out.println("Cat Scratch, Draw.");
demo.playerChange();
}
}
I am not sure how to play the game right, every time I input numbers when I run it, I get the error code. What have I done wrong with this? The code can be compiled and runs and displays the board but when I go to put in the place I want the x or the o to go I get the error code " invalid Top level statement "
You have to use the Scanner class to make a player input using import java.util.Scanner then storing the input. After the import it going to look like this:
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
And you have to manage the sc.nextInt() result, in this example the input variable.

Categories