Unable to determine win condition in Tic Tac Toe - java

I have been trying to get my tic tac toe program to work for a while now.
At the moment I'm stuck in the win condition.
The game can now end as a draw but no player (human || computer) can win it.
I need to determine the winning conditions (horizontal, vertical, across) in playerHasWon but have no idea how to implement them.
import java.util.Scanner;
import java.util.Random;
public class NewTicTacToe {
public static final int DRAW = 0;
public static final int COMPUTER = 1;
public static final int PLAYER = 2;
public static final char PLAYER_MARK = 'X';
public static final char COMPUTER_MARK = 'O';
public static int size;
public static String[][] board;
public static int score = 0;
public static Scanner scan = new Scanner(System.in);
/**
* Creates base for the game.
*
* #param args the command line parameters. Not used.
*/
public static void main(String[] args) {
while (true) {
System.out.println("Select board size");
System.out.print("[int]: ");
try {
size = Integer.parseInt(scan.nextLine());
} catch (Exception e) {
System.out.println("You can't do that.");
continue;
}
break;
}
int[] move = {};
board = new String[size][size];
setupBoard();
int i = 1;
loop:
while (true) {
if (i % 2 == 1) {
displayBoard();
move = getMove();
} else {
computerTurn();
}
switch (isGameFinished(move)) {
case PLAYER:
System.err.println("YOU WIN!");
break loop;
case COMPUTER:
System.err.println("Computer WINS!\nYOU LOSE!");
break loop;
case DRAW:
System.err.println("IT'S A DRAW");
break loop;
}
i++;
}
}
private static int isGameFinished(int[] move) {
if (isDraw()) {
return DRAW;
} else if (playerHasWon(board, move, COMPUTER_MARK)) {
return COMPUTER;
} else if (playerHasWon(board, move, PLAYER_MARK)) {
return PLAYER;
}
return -1;
}
/**
* Checks for win.
*
* #param board
* #return if the game is won.
*/
public static boolean playerHasWon(String[][] board, int[] move,
char playerMark) { //playermark x || o
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
if (board[i][j].equals(playerMark)) {
score++;
} else {
score = 0;
}
if (score == 5) {
return true;
}
}
}
return hasWon;
}
/**
* Checks for draws.
*
* #return if this game is a draw
*/
public static boolean isDraw() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == " ") {
return false;
}
}
}
return true;
}
/**
* Displays the board.
*
*
*/
public static void displayBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.printf("[%s]", board[i][j]);
}
System.out.println();
}
}
/**
* Displays the board.
*
*
*/
public static void setupBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = " ";
}
}
}
/*
* Takes in userinput and sends it to isValidPlay.
*
*
*/
public static int[] getMove() {
Scanner sc = new Scanner(System.in);
System.out.println("Your turn:");
while (true) {
try {
System.out.printf("ROW: [0-%d]: ", size - 1);
int x = Integer.parseInt(sc.nextLine());
System.out.printf("COL: [0-%d]: ", size - 1);
int y = Integer.parseInt(sc.nextLine());
if (isValidPlay(x, y)) {
board[x][y] = "" + PLAYER_MARK;
return new int[]{x, y};
}
} catch (Exception e) {
System.out.println("You can't do that.");
}
return null;
}
}
/*
* Randomizes computer's turn - where it inputs the mark 'O'.
*
*
*/
public static void computerTurn() {
Random rgen = new Random(); // Random number generator
while (true) {
int x = (int) (Math.random() * size);
int y = (int) (Math.random() * size);
if (isValidPlay(x, y)) {
board[x][y] = "" + COMPUTER_MARK;
break;
}
}
}
/**
* Checks if the move is possible.
*
* #param inX
* #param inY
* #return
*/
public static boolean isValidPlay(int inX, int inY) {
// Play is out of bounds and thus not valid.
if ((inX >= size) || (inY >= size)) {
return false;
}
// Checks if a play have already been made at the location,
// and the location is thus invalid.
return (board[inX][inY] == " ");
}
}
// End of file

You could use a different approach to check for victories. Maybe split in three parts? Horizontal, vertical, and diagonals?
I made some changes to your code to give an example:
public static boolean playerHasWon(String[][] board, int[] move,
String playerMark) { //playermark x || o
//Horizontal check
for (int i = 0; i < size; i++) {
if (board[i][0].equals(playerMark)) {
int j;
for (j = 1; j < size; j++) {
if (!board[i][j].equals(playerMark)) {
break;
}
}
if (j == size) {
return true;
}
}
}
//Vertical check
for (int i = 0; i < size; i++) {
if (board[0][i].equals(playerMark)) {
int j;
for (j = 1; j < size; j++) {
if (!board[j][i].equals(playerMark)) {
break;
}
}
if (j == size) {
return true;
}
}
}
//Diagonals
int i;
for (i = 0; i < size; i++) {
if (!board[i][i].equals(playerMark)) {
break;
}
}
if (i == size) {
return true;
}
for (i = 0; i < size; i++) {
if (!board[i][(size - 1) - i].equals(playerMark)) {
break;
}
}
if (i == size) {
return true;
}
return false;
}
Note that you have to change the playerMarks from char to String.
Also, I suggest displaying the board just before announcing who won. That way, if the computer wins, you can see its last move

Related

Module and how to use it in the situation below

public class AirplaneLab
{
private int [][] first;
private int [][] economy;
private boolean [] seat;
private boolean okay;
private boolean okayokay;
public AirplaneLab()
{
}
public AirplaneLab(int [][] first1, int [][] economy1)
{
}
public boolean viewFirstClass(boolean set[], int [][] first, int [][] economy)
{
if (okay = true)
{
boolean seating1[] = new boolean[20];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
if(seat[((j + 1) + (i * 4)) - 1])
{
System.out.print("x ");
seating1[i * j] = true;
}
else
{
System.out.print("o ");
seating1[i * j] = flase;
}
}
System.out.println();
}
System.out.println("The x's are the sets that are taken, o's are not");
return seating1[];
}
else
{
return false;
}
}
public boolean viewEconomyClass(boolean set[], int [][] first, int [][] economy)
{
if (okayokay = true)
{
boolean seating2[] = new boolean[30];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 3; j++)
{
if(seat[((j + 1) + (i * 3)) - 1])
{
System.out.print("x ");
seating2[i * j] = true;
}
else
{
System.out.print("o ");
seating2[i * j] = false;
}
}
System.out.println();
}
System.out.println("The x's are the sets that are taken, o's are not");
return seating2[];
}
else
{
return false;
}
}
public void decision()
{
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Please choose an option:");
System.out.println("1 for “booking in first class”");
System.out.println("2 for “booing in economy class”");
System.out.println("3 to view seating chart for first class ");
System.out.println("4 to view seating chart for economy class");
System.out.println("0 to exit");
System.out.print("? ");
while(true)
{
int mOpt = input.nextInt();
if ((mOpt == 1) || (mOpt == 3))
{
if (mOpt == 1)
{
okay = true;
System.out.println("Based on the following setting arrangement, please pick a window middle or end seat");
viewFirstClass(boolean set[], int [][] first, int [][] economy);
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
if (seating1[i * j] == true)
{
if ((i * j) ________________)
}
}
}
}
}
}
}
}
In the code above, where the blank is:
The last if statement before all of the closed brackets:
I was wondering how you would use module there.
Let's say I wanted to do (i * j) module of 4; how would I do that? Can you fill in the blank? Thank you for your help!
If you are looking for some thing (modulo) like
if ((i * j) mod 4 )
in java ,the syntax would be
if ((i * j) % 4 )

How do I determine a winner in my Tic Tac Toe program

Earlier post: How do I make my tictactoe program scalable
I have tried to make a Tic Tac Toe program (human vs computer) scalable (the board size can be changed). I had major problems earlier but fixed most of them.
The game's rules are basic Tic Tac Toe but one different rule is that no matter how large the board is (when >= 5) the player or computer only need five marks in a row to win.
Now the only gamebreaking problem with my program is determining who wins the game. It is only possible that the game ends a 'draw' at the moment. (Also I have not implemented the ">= 5" yet).
Specific problem explanation is that I need to determine a winner and end screen for something like "computer wins" and/or "player wins".
package tictactoe;
import java.util.Scanner;
import java.util.Random;
public class TicTacToe {
public static int size;
public static char[][] board;
public static int score = 0;
public static Scanner scan = new Scanner(System.in);
/**
* Creates base for the game.
*
* #param args the command line parameters. Not used.
*/
public static void main(String[] args) {
System.out.println("Select board size");
System.out.print("[int]: ");
size = Integer.parseInt(scan.nextLine());
board = new char[size][size];
setupBoard();
int i = 1;
while (true) {
if (i % 2 == 1) {
displayBoard();
getMove();
} else {
computerTurn();
}
// isWon()
if (isDraw()) {
System.err.println("Draw!");
break;
}
i++;
}
}
/**
* Checks for draws.
*
* #return if this game is a draw
*/
public static boolean isDraw() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}
/**
* Displays the board.
*
*
*/
public static void displayBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.printf("[%s]", board[i][j]);
}
System.out.println();
}
}
/**
* Displays the board.
*
*
*/
public static void setupBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = ' ';
}
}
}
/*
* Checks if the move is allowed.
*
*
*/
public static void getMove() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.printf("ROW: [0-%d]: ", size - 1);
int x = Integer.parseInt(sc.nextLine());
System.out.printf("COL: [0-%d]: ", size - 1);
int y = Integer.parseInt(sc.nextLine());
if (isValidPlay(x, y)) {
board[x][y] = 'X';
break;
}
}
}
/*
* Randomizes computer's turn - where it inputs the mark 'O'.
*
*
*/
public static void computerTurn() {
Random rgen = new Random(); // Random number generator
while (true) {
int x = (int) (Math.random() * size);
int y = (int) (Math.random() * size);
if (isValidPlay(x, y)) {
board[x][y] = 'O';
break;
}
}
}
/**
* Checks if the move is possible.
*
* #param inX
* #param inY
* #return
*/
public static boolean isValidPlay(int inX, int inY) {
// Play is out of bounds and thus not valid.
if ((inX >= size) || (inY >= size)) {
return false;
}
// Checks if a play have already been made at the location,
// and the location is thus invalid.
return (board[inX][inY] == ' ');
}
}
You have already the loop to play, so, in each iteration, in the same way you check if the game isDraw(), check also if some of the players won:
while (true) {
if (i % 2 == 1) {
displayBoard();
getMove();
} else {
computerTurn();
}
// isWon()
if (isDraw()) {
System.err.println("Draw!");
break;
} else if (playerHasWon()){
System.err.println("YOU WIN!");
break;
} else if (computerHasWon()) {
System.err.println("Computer WINS!\nYOU LOOSE!!");
break;
}
i++;
}
After create the needed methods:
public static boolean playerHasWon() {
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
}
}
return hasWon ;
}
public static boolean computerHasWon() {
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
}
}
return hasWon ;
}
Next question of course is HOW DO I CREATE THIS METHODS?? dunno if you have problems with this, but doing a fast check here here and here you will find some ideas.
ADD ON:
In order to clarify, I would make a function returning an int instead booleans, to check if the game is finished using some constants:
private final int DRAW = 0;
private final int COMPUTER = 1;
private final int PLAYER = 2;
private int isGameFinished() {
if (isDraw()) return DRAW;
else if (computerHasWon()) return COMPUTER;
else if (playerHasWon()) return PLAYER;
}
Then simply check with a switch case (check here how to break the while insite the while)
loop: while (true) {
// other stufff
switch (isGameFinished()) {
case PLAYER:
System.err.println("YOU WIN!");
break loop;
case COMPUTER:
System.err.println("Computer WINS!\nYOU LOOSE!!");
break loop;
case DRW:
System.err.println("IT'S A DRAW");
break loop;
}

How to fix Tic Tac Toe winner determination

Every time I run my Tic Tac Toe program I can create the board and do my first turn.
After the first turn, the game just ends as: "IT'S A DRAW", which is one of the three ending possibilities. This just happens before the computer can even make his own turn.
Another problem in my program is that the scanner user input limit(er) is not working (at the end of the code). If user inputs i.e a letter instead of int, the program crashes.
package newtictactoe;
import java.util.Scanner;
import java.util.Random;
public class NewTicTacToe {
public static final int DRAW = 0;
public static final int COMPUTER = 1;
public static final int PLAYER = 2;
public static int size;
public static char[][] board;
public static int score = 0;
public static Scanner scan = new Scanner(System.in);
/**
* Creates base for the game.
*
* #param args the command line parameters. Not used.
*/
public static void main(String[] args) {
System.out.println("Select board size");
System.out.print("[int]: ");
size = Integer.parseInt(scan.nextLine());
board = new char[size][size];
setupBoard();
int i = 1;
loop:
while (true) {
if (i % 2 == 1) {
displayBoard();
getMove();
} else {
computerTurn();
}
switch (isGameFinished()) {
case PLAYER:
System.err.println("YOU WIN!");
break loop;
case COMPUTER:
System.err.println("Computer WINS!\nYOU LOOSE!!");
break loop;
case DRAW:
System.err.println("IT'S A DRAW");
break loop;
}
i++;
}
}
private static int isGameFinished() {
if (isDraw()) {
return DRAW;
} else if (computerHasWon()) {
return COMPUTER;
} else if (playerHasWon()) {
return PLAYER;
}
return 0;
}
/**
* Checks for computer's win.
*
* #return if this game is won by computer.
*/
public static boolean playerHasWon() {
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
}
}
return hasWon;
}
/**
* Checks for player's win.
*
* #return if this game is won by computer.
*/
public static boolean computerHasWon() {
boolean hasWon = false;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// check if 5 in a line
}
}
return hasWon;
}
/**
* Checks for draws.
*
* #return if this game is a draw
*/
public static boolean isDraw() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (board[i][j] == ' ') {
return false;
}
}
}
return true;
}
/**
* Displays the board.
*
*
*/
public static void displayBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.printf("[%s]", board[i][j]);
}
System.out.println();
}
}
/**
* Displays the board.
*
*
*/
public static void setupBoard() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = ' ';
}
}
}
/*
* Checks if the move is allowed.
*
*
*/
public static void getMove() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.printf("ROW: [0-%d]: ", size - 1);
int x = Integer.parseInt(sc.nextLine());
System.out.printf("COL: [0-%d]: ", size - 1);
int y = Integer.parseInt(sc.nextLine());
if (isValidPlay(x, y)) {
board[x][y] = 'X';
break;
}
}
}
/*
* Randomizes computer's turn - where it inputs the mark 'O'.
*
*
*/
public static void computerTurn() {
Random rgen = new Random(); // Random number generator
while (true) {
int x = (int) (Math.random() * size);
int y = (int) (Math.random() * size);
if (isValidPlay(x, y)) {
board[x][y] = 'O';
break;
}
}
}
/**
* Checks if the move is possible.
*
* #param inX
* #param inY
* #return
*/
public static boolean isValidPlay(int inX, int inY) {
// Play is out of bounds and thus not valid.
if ((inX >= size) || (inY >= size)) {
return false;
}
// Checks if a play have already been made at the location,
// and the location is thus invalid.
return (board[inX][inY] == ' ');
}
These last two methods in the code check if the scanner input is valid but they don't work and I don't know why.
/**
* Checks if user input is valid
*
* #param scan
* #param prompt
* #return
*/
public static String getInput(Scanner scan, String prompt) {
System.out.print(prompt); // Tell user what to input
String text = "Enter one integer value i.e 5.";
while (true) { // Keeps on looping until valid input
text = scan.nextLine();
if (isInteger(text)) // Checks input is int
{
break; // Exit loop
}
System.out.print("Try again, " + prompt); // If invalid
}
return text; // Return valid user input
}
/**
* Checks if input string is int.
*
* #param str
* #return
*/
public static boolean isInteger(String str) {
try {
Integer.parseInt(str); // If this succeeds the input is int
return true;
} catch (NumberFormatException e) {
return false; // If not int
}
}
}
Your isGameFinished() method returns 0 by default (when the game is not over), but your DRAW constant is also 0. Try to change it to return (for example) -1 when the game is not over.
A nicer solution would be to have one method isGameFinished() that would return boolean to indicate if the game is over, and another method getWinner() which would return the winner (DRAW or COMPUTER or PLAYER), and will only be called if isGameFinished() returns true.
Your getMove method should catch NumberFormatException, which can be thrown by Integer.parseInt.

Show certain amount of prime numbers

I have been working on some java code to show prime numbers . I have got as far as having it show all prime numbers between 0 and 100 .
How would I make it so that I could set a variable to say 20 and it would show me the first 20 prime numbers.
My Code :
public class PrimeNumber {
/**
* #param args the command line arguments
*/
private static boolean prime = true;
private static int count = 20;
public static void main(String[] args) {
for (int i = 2; i < 100; i++) {
for (int j = 2; j < 100; j++) {
if(i == j)
{
continue;
}
if (i % j == 0) {
prime = false;
break;
} else {
prime = true;
}
}
if (prime) {
System.out.println(i + " is a Prime:");
}
}
}
}
Here is the simplest possible implementation, for the OPs code.
Decrease the count variable and check it in the outer for loop till it reaches zero. Also Inner for-loop should check only till the current value of (i/2 + 1). The other half you can always skip, the value will will divide the number will be i itself.
public class PrimeNumber {
/**
* #param args the command line arguments
*/
private static boolean prime = true;
private static int count = 20;
public static void main(String[] args) {
for (int i = 2; count>0; i++) {
for (int j = 2; j < i/2 + 1; j++) {
if (i % j == 0) {
prime = false;
break;
} else {
prime = true;
}
}
if (prime) {
System.out.println(i + " is a Prime:");
count--;
}
}
}
}
Check below code for your answer:
public class PrimeNumber {
private static boolean prime = true;
private static int count = 20;
public static void main(String[] args) {
for (int i = 2; i < count+1; i++) {
for (int j = 2; j < 100; j++) {
if(i == j)
{
continue;
}
if (i % j == 0) {
prime = false;
break;
} else {
prime = true;
}
}
if (prime) {
System.out.println(i + " is a Prime:");
}}}}

Java Antwalk - 2D Arrays

There is a 2D array with dimensions 9x9. The ant starts in cell (5,5) , or the center. It moves randomly north, south, east, or west. The ant stops walking when it falls off the grid. The space where it falls is marked by an X. In the matrix, it should display the amount of times the ant visited each cell and the number of moves the ant took to fall off.
I'm not sure where to start or how to go about it. Like how to make the north, south, west, east methods.
Here's what I have so far.
public static void main(String [] args)
{
int [][] grid = new int[9][9];
for (int r = 0; r< grid.length; r++)
{
for (int c = 0 ; c<grid[0].length; c++)
{
grid[r][c] = 0;
}
}
boolean test = true;
while(test)
{
int random = (int)Math.random()*4+1;
if (random == 1)
{
}
else if (random == 2)
{
}
else if (random == 3)
{
}
else if (random == 4)
{
}
}
}
You've got the right idea, now what you want to do is move in the direction specified by random, and increment the value in your array. Here is how I would do it.
int count = 0;
int x = 4;
int y = 4; // arrays are 0 based
while(true)
{
int random = (int)Math.random()*4+1;
if (random == 1)
{
x--; // move left
}
else if (random == 2)
{
x++; // move right
}
else if (random == 3)
{
y--; // move down
}
else if (random == 4)
{
y++; // move up
}
if(x < 0 || y < 0 || x >= grid.length || y >= grid[x].length) break;
count++;
grid[x][y]++;
}
System.out.println(count); // number of moves before it fell
Here is a random walk of an ant on the grid. See comments in the code. The ant walks STEPS on a predefined size grid.
import java.util.Random;
public class Ants {
/** height and width of the grid */
public static final int HEIGHT = 9 ;
public static final int WIDTH = 9 ;
/** NOVAL means "no value" */
public static final int NOVAL = -1;
/** world directions */
public static final int NORTH = 0 ;
public static final int WEST = 1 ;
public static final int SOUTH = 2 ;
public static final int EAST = 3 ;
/** how many steps for ant to walk */
public static final int STEPS = 10;
/** where does the ant start it's walk */
public static final int START_X = 5;
public static final int START_Y = 5;
/** printing related */
public static final String ANT = "ANT";
private static final Random random = new Random();
/** grid for ant to walk on */
static int[] grid;
/** get height of the grid */
static int getHeight() {
return HEIGHT;
}
/** get width of the grid */
static int getWidth() {
return WIDTH;
}
/** size of the grid */
static int getSize() {
return getWidth()*getHeight();
}
/** coordinates are converted to one dimension */
/** #return index from coordinates. */
static int idx(int x, int y) {
return y*getWidth() + x;
}
/** get x coordinate of idx */
static int x(int idx) {
return idx % getWidth();
}
/** get y coordinate of idx */
static int y(int idx) {
return (idx - x(idx)) / getWidth();
}
/** get cell */
static int getCell(int idx) {
return grid[idx];
}
static int getCell(int x, int y) {
return getCell(
idx(x,y));
}
static void setCell(int idx, int value) {
grid[idx] = value;
}
/** init array with some value */
private static void initArr(int[] arr, int value) {
for (int i = 0; i < arr.length; i++) {
arr[i] = value;
}
}
/**
* #return adjancted cells indexes.
*/
public static int[] adjanctedTo(int idx) {
int[] adj = new int[4];
initArr(adj, NOVAL);
int x = x(idx);
int y = y(idx);
/** Is North available? */
if (y - 1 >= 0) {
adj[NORTH] = idx(x,y-1);
}
/** West? */
if (x - 1 >= 0) {
adj[WEST] = idx(x-1, y);
}
/** South? */
if (y + 1 < getHeight()) {
adj[SOUTH] = idx(x,y+1);
}
/** East? */
if (x + 1 < getWidth()) {
adj[EAST] = idx(x+1, y);
}
return adj;
}
/** picks random value from array */
public static int pick(int[] arr) {
int ret = NOVAL;
int idx = random.nextInt(4);
for (int i = idx;; i++) {
if (NOVAL != arr[i]) {
ret = arr[i];
break;
}
if (3 == i) {
/** cycle if not yet found a NOVAL value in array */
i = 0;
}
}
return ret;
}
public static void main(String[] args) {
/** init grid */
grid = new int[getSize()];
int nextStep = NOVAL;
int current = idx(START_X, START_Y);
setVisited(current);
for (int s = 0; s < STEPS; s++) {
System.out.println("STEP "+s);
System.out.println(
"Standing #" + position(current) + ".");
printGrid(current);
nextStep = pick(
adjanctedTo(current));
System.out.println(
"Moving to " + position(nextStep));
setVisited(current);
printGrid(nextStep);
current = nextStep;
}
}
public static void setVisited(int idx) {
setCell(idx, getCell(idx)+1);
}
public static String position(int idx) {
return idx+"("+x(idx) + ";" + y(idx) +")";
}
public static void printGrid(int antPosition) {
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
if (idx(x,y) == antPosition) {
System.out.print(ANT);
} else {
System.out.format(
"%2d|",
getCell(x,y));
}
}
System.out.println();
}
}
}
public static void main(String[] args) {
String[][] sim = ant(3,3);
for (int i = 0; i < sim.length; i++) {
for (int j = 0; j < sim[i].length; j++) {
System.out.print(sim[i][j] + " ");
}
System.out.println();
}
}
static String[][] ant(int x, int y) {
if(x<0||x>=9||y<0||y>=9){
return null;
}
int[] rn = {-1, 1}; //next ant random direction , you can include 0 if you wanted
String[][] mat = new String[9][9];
for (int i = 0; i < 9; i++) {
Arrays.fill(mat[i], "0");//fill array with 0 to avoid null default value
}
int a = x, b = y; // a and b are the Cartesian coordinates of the ant
while (true) {
mat[a][b] = String.valueOf(Integer.parseInt(mat[a][b]) + 1);
int newx = a + rn[(int) (Math.random() * rn.length)];
int newy = b + rn[(int) (Math.random() * rn.length)];
//these are the ant new coordinates , we have to check them before going to them
if (newx < 0 || newx >= 9 || newy < 0 || newy >= 9) {
mat[a][b] = "X";
return mat;
}
a = newx;
b = newy;
}
}

Categories