I'm trying to implement The Game of Life in java, as an exercise to learn this language.
Unfortunately I have a problem, as I don't seem able to make this program run correctly.
I implemented a torodial sum (the plane is a donut) with no problem:
int SumNeighbours (int i, int j) {
int value = 0;
value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+world[( i - 1 + row ) % row][( j + 1 ) % column];
value = value + world[i][( j - 1 + column ) % column] + world[i][( j + 1 ) % column];
value = value + world[( i + 1 ) % row][( j - 1 + column ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];
return value;
}
And it sums correctly when I test it:
void NextWorldTest () {
int count;
int [][] nextWorld = new int[row][row];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
}
System.out.println();
}
world=nextWorld;
}
Unfortunately when I add the conditions of game of life (born/death) the program stop working correctly, as it seems not able anymore to count correctly the alive cells in the neighborhood. It counts where there are none, and it doesn't count when there are some. E.g.: it doesn't count the one below some living cells.
It's a very odd behaviour, and it's been giving me a headache for 3 days now... maybe I'm missing something basic about variables?
Here you can find the class.
void NextWorld () {
int count;
int [][] nextWorld = new int[row][column];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
if ( ( world[i][j] == 0) && ( count == 3 ) ) {
nextWorld[i][j] = 1;
} else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {
nextWorld[i][j] = 1;
} else {
nextWorld[i][j]=0;
}
}
System.out.println();
}
world=nextWorld;
}
}
Am I doing something wrong?
Below you can find the full package.
package com.GaOL;
public class GameWorld {
int [][] world;
int row;
int column;
public int GetRow() {
return row;
}
public int GetColumn() {
return column;
}
public int GetWorld (int i, int j) {
return world[i][j];
}
void RandomGen (int size, double p1) {
double randomCell;
row = size;
column = size;
world = new int[row][column];
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
randomCell=Math.random();
if (randomCell < 1-p1) {
world[i][j] = 0;
} else {
world[i][j] = 1;
}
}
}
}
void printToConsole() {
double test = 0;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
if ( world[i][j] == 0 ) {
System.out.print(" ");
} else {
System.out.print(" * ");
test++;
}
}
System.out.println("");
}
System.out.println("ratio is " + test/(row*column));
}
int SumNeighbours (int i, int j) {
int value = 0;
value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+world[( i - 1 + row ) % row][( j + 1 ) % column];
value = value + world[i][( j - 1 + column ) % column] + world[i][( j + 1 ) % column];
value = value + world[( i + 1 ) % row][( j - 1 + column ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];
return value;
}
void NextWorldTest () {
int count;
int [][] nextWorld = new int[row][row];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
}
System.out.println();
}
world=nextWorld;
}
void NextWorld () {
int count;
int [][] nextWorld = new int[row][column];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
if ( ( world[i][j] == 0) && ( count == 3 ) ) {
nextWorld[i][j] = 1;
} else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {
nextWorld[i][j] = 1;
} else {
nextWorld[i][j]=0;
}
}
System.out.println();
}
world=nextWorld;
}
}
and here the test class:
package com.GaOL;
public class GameTestClass {
public static void main(String[] args) {
GameWorld prova = new GameWorld();
prova.RandomGen(10, 0.02);
for (int i=0; i<3; i++) {
prova.printToConsole();
prova.NextWorld();
}
}
}
I think, problem is in arrays assignments. What are you doing here?
int [][] nextWorld = new int[row][row];
nextWorld = world;
you should create new array [row][column] and then fill it according data from world.
And more
in Java you can do smt like anArray.length and anArray[0].length, so you do not need row and column variables
you should name all you method starting with low letter, like sumNeighbours() instead of SumNeighbours()
upd:there was typo in second advise: fixed
Related
Hello so am trying to create a 2D array of int with random number of rows and columns and a random starting and ending points using java to apply the A* algorithm on it.
When i add {S} and {E} to define the tow points and print it there are numbers outside of the 2D array printed.
`Random rand = new Random();
int min = 2, max = 10;
// a random number of rows and columns
int a = (int)(Math.random() * (max - min + 1)) + min;
// the location of the starting point.
int row_start = rand.nextInt(a);
int col_start = rand.nextInt(a);
// the location of the ending point.
int row_end = rand.nextInt(a);
int col_end = rand.nextInt(a);
int [][] M = new int [a][a];
public void create() {
//empty: 0; grass: 1; sand: 2; water: 3; wall: 4.
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
M[i][j] = rand.nextInt(5);
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
System.out.print(" " +M[i][j] + "\t");
if(row_start == i && col_start == j) {
System.out.print("{S}" + "\t");
}
if(row_end == i && col_end == j) {
System.out.print("{E}" + "\t");
}
}
System.out.print("\n");
}
}`
the output looks like this:
1 0 4 0
2 {S} 1 2 2
4 4 {E} 0 3
2 0 3 3
the 2 and 3 shouldn't appear there.
The problem is that you always print m[i][j].
What you need is to only print m[i][j] when i and j are not S and E positions. When i and j are S and E positions, print S or E. Otherwise, print m[i][j].
if(row_start == i && col_start == j) {
System.out.print("{S}" + "\t");
} else if(row_end == i && col_end == j) {
System.out.print("{E}" + "\t");
} else {
System.out.print(" " +M[i][j] + "\t");
}
I cant get credit card numbers containing hymens as INVALID such as 4003-6000-0000-0014 must give me INVALID but its giving me errors of string.
public class prog {
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in) ;
System.out.println("How many credit card you want to check?");
int numOfCredit = userInput.nextInt() ;
int creditnumbers[] = new int[numOfCredit] ;
for (int i = 0 ; i<numOfCredit ; i++)
{
System.out.println("Enter credit card number "+(i+1)+": ") ;
String creditNumber = userInput.next() ;
validateCreditCardNumber(creditNumber);
}
}
private static void validateCreditCardNumber(String str) { // function to check credit numbers
int[] ints = new int[str.length()];
for (int i = 0; i < str.length(); i++) {
ints[i] = Integer.parseInt(str.substring(i, i + 1));
}
for (int i = ints.length - 2; i >= 0; i = i - 2) {
int j = ints[i];
j = j * 2;
if (j > 9) {
j = j % 10 + 1;
}
ints[i] = j;
}
int sum = 0;
for (int i = 0; i < ints.length; i++)
{
sum += ints[i];
}
if (sum % 10 == 0)
{
System.out.println("VALID");
}
else
{
System.out.println("INVALID");
}
}
}
I get these errors after running with hymens :
Exception in thread "main" java.lang.NumberFormatException: For input string: "-"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:642)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at testing/testing.prog.validateCreditCardNumber(prog.java:33)
at testing/testing.prog.main(prog.java:22)
you can replace in your string "-" with "" (blank) and then apply this function:
String card = "4003-6000-0000-0014";
Boolean t = check(card.replace("-",""));
public static boolean check(String ccNumber)
{
int sum = 0;
boolean alternate = false;
for (int i = ccNumber.length() - 1; i >= 0; i--)
{
int n = Integer.parseInt(ccNumber.substring(i, i + 1));
if (alternate)
{
n *= 2;
if (n > 9)
{
n = (n % 10) + 1;
}
}
sum += n;
alternate = !alternate;
}
return (sum % 10 == 0);
}
Made some edits to the code to try and figure out why my X's [-1] are not being included in finding my average for that row. That is throwing of my averages. Any idea why It is not counting my -1's?
output[expected]:
USER INPUT: 3
O O O
X X X
X X X
TOTAL OPENNESS OF [I][J] = 1
TOTAL OPENNESS OF [I][J+1] = 2
TOTAL OPENNESS OF [I][J+2] = 1
TOTAL SUM AVERAGE FOR THAT ROW = 1.3
HOWEVER..FOR ROW 2 AND ROW 3
TOTAL SUM AVERAGE FOR THOSE ROWS = 0
WHICH IS INCORRECT IT SHOULD = -1
public static void openfactor(char[][] mazeValue, int n){
for(int i = 1; i<=n; i++)
{
double rowAvg=0;
double totalRowAvg=0;
for(int j=1;j<=n;j++)
{
int count=0;
int totalOpeness=0;
int totalRowOpeness = 0;
//double rowAvg=0;
if(mazeValue[i][j]=='X'){
System.out.println("tHIS IS AN X FOR : [" + i + "]" +"[" + j + "] IS -1 ");
count = -1;
}
else
{
//YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
if( j-1>=1)
{
if(mazeValue[i][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1 && j-1>=1)
{
if(mazeValue[i-1][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=1)
{
if(mazeValue[i-1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n)
{
if(mazeValue[i][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<=n && i+1<=n)
{
if(mazeValue[i+1][j+1]=='O')
count++;
}
if (i+1<=n)
{
if(mazeValue[i+1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j-1>=1 && i+1<=n)
{
if(mazeValue[i+1][j-1]=='O')
count++;
}
if(i-1>=1 && j+1<=n)
{
if(mazeValue[i-1][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
totalOpeness = totalOpeness +count;
System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "] IS " +totalOpeness);
totalRowOpeness = totalRowOpeness + totalOpeness;
//}//eND OF iF CONDITION\
}
rowAvg = (double)totalRowOpeness/(double)n;
System.out.println("ROW AVERAGE: "+rowAvg);
totalRowAvg = totalRowAvg + rowAvg;
System.out.println("SUM ROW AVERAGE: "+totalRowAvg);
}
System.out.println("TOTAL SUM ROW AVERAGE: " +totalRowAvg);
}
}
public static void printMaze(char mazeValue[][]) {
System.out.println("MAZE");
for (int i = 1; i < mazeValue.length; i++) {
for (int j = 1; j < mazeValue[i].length; j++) {
System.out.printf("%5c", mazeValue[i][j]);
}
System.out.printf("\n");
}
}
public static void main(String[] args) {
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
char[][] mazeValue = new char[n + 1][n + 1];
System.out.println("ENTER A PATH: ");
for (int i = 0; i < mazeValue.length; i++) {
for (int j = 0; j < mazeValue[i].length; j++) {
if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
mazeValue[i][j] = 'X';
else {
mazeValue[i][j] = kbd.next().charAt(0);
}
}
}
printMaze(mazeValue);
horizontalPath(mazeValue, n);
System.out.println(" ");
verticalPath(mazeValue,n);
System.out.println(" ");
openfactor(mazeValue, n);
}
}
I do not completely understand what u want to accomplished but I am going to to assume you want to find repeated values, do this using some search algorithm below is an example of a binary search. Hope it helps.
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " + (middle + 1) + ".");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
System.out.println(search + " is not present in the list.\n");
}
}
Here's the complete code for your request. you need to reorder your if statements a little bit your logic was right:
and here is the output :
MAZE
O O X
O O O
X X O
TOTAL OPENESS FOR : [0][0] IS 3
TOTAL OPENESS FOR : [0][1] IS 4
THERE IS AN X HERE FOR : [0][2]
Average of O's in this row is : 66.66667%
TOTAL OPENESS FOR : [1][0] IS 3
TOTAL OPENESS FOR : [1][1] IS 5
TOTAL OPENESS FOR : [1][2] IS 3
Average of O's in this row is : 100.0%
THERE IS AN X HERE FOR : [2][0]
THERE IS AN X HERE FOR : [2][1]
TOTAL OPENESS FOR : [2][2] IS 2
Average of O's in this row is : 33.333336%
here's the code:
import java.util.Scanner;
public class sof {
public static boolean IsOutOfBound(int i, int j, int n)
{
if (i-1<1 || j-1<1 || i+1>n || j+1>n)
return true;
else
return false;
}
public static void openfactor(char[][] mazeValue, int n)
{
for(int i = 0; i<n; i++)
{
int TotalCounts=0;
for(int j=0;j<n;j++)
{
int count=0;
if(mazeValue[i][j]=='X'){
System.out.println("THERE IS AN X HERE FOR : [" + i + "]" +"[" + j + "] ");
//TotalCounts--;
}
else
{
//YOU NEED TO VERIFY THAT J IS NOT OUT OF BOUND
if( j-1>=0)
{
if(mazeValue[i][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=0 && j-1>=0)
{
if(mazeValue[i-1][j-1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(i-1>=0)
{
if(mazeValue[i-1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<n)
{
if(mazeValue[i][j+1]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j+1<n && i+1<n)
{
if(mazeValue[i+1][j+1]=='O')
count++;
}
if (i+1<n)
{
if(mazeValue[i+1][j]=='O')
count++;
}
// System.out.println("cout: "+count);
if(j-1>=0 && i+1<n)
{
if(mazeValue[i+1][j-1]=='O')
count++;
}
if(i-1>=0 && j+1<n)
{
if(mazeValue[j+1][i-1]=='O')
count++;
}
// System.out.println("cout: "+count);
//totalOpeness = totalOpeness +count;
System.out.println("TOTAL OPENESS FOR : [" + i + "]" +"[" + j + "] IS " + count);
TotalCounts++;
}//END OF else CONDITION
}//End of J loop
float Average = ((float)TotalCounts/(float)n) * 100;
System.out.println("Average of O's in this row is : " + Average+ "%");
}//End of I loop
}
public static void printMaze(char mazeValue[][],int n) {
System.out.println("MAZE");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%5c", mazeValue[i][j]);
}
System.out.printf("\n");
}
}
public static void main(String[] args) {
// TODO code application logic here
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
char[][] mazeValue = new char[n][n];
System.out.println("ENTER A PATH: ");
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
//if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
// mazeValue[i][j] = 'X';
// else {
mazeValue[i][j] = kbd.next().charAt(0);
// }
}
}
printMaze(mazeValue,n);
openfactor(mazeValue, n);
}
}
What I need is a little modification to my code so that every part of my hollow diamond prints a letter of the word "HURRICANE"
My code is:
String st1 = "HURRICANE";
int a = 0;
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(a)); //needs change
} else {
System.out.print(' ');
}
}
System.out.println();
}
for (int i = 2; i <= 5; i++) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(a)); //needs change
} else {
System.out.print(' ');
}
}
System.out.println();
}
The output comes out as:
H
H H
H H
H H
H H
H H
H H
H H
H
I need to modify my "charAt" statement a little so it comes out to be:
H
U U
R R
R R
I I
C C
A A
N N
E
How should I make my print statement?
It's worth noting that the example provided only works for Strings the same length as "HURRICANE". A superior solution would work for all strings.
Partial solution for you to complete, since I guess it's your coursework and I don't want you to copy / paste / fail exams :P
public static void main(String[] args) {
String st1 = "HURRICANE";
char[] st1CharArray = st1.toCharArray();
int maxSpaces = st1CharArray.length / 2 + 1;
for (int i = 0; i <= st1CharArray.length / 2; i++) {
if (i == 0) {
System.out.println(getSpacesString(maxSpaces) + st1CharArray[i]);
} else {
System.out.println(getSpacesString(maxSpaces - i)
+ st1CharArray[i] + getSpacesString(i * 2 - 1)
+ st1CharArray[i]);
}
}
// Loop from st1CharArray.length / 2 + 1 and get the second half done.
}
private static String getSpacesString(int numberOfSpaces) {
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < numberOfSpaces; i++) {
strBuilder.append(" ");
}
return strBuilder.toString();
}
//: Playground - noun: a place where people can play
import UIKit
var name : String = "HURRICANE"
var dimensions : Int = name.count - 1
var k : Int = 0
for rows in 0...dimensions{
for columns in 0...dimensions{
k = abs( (dimensions/2) - rows )
if columns == k || columns == dimensions - k{
print(Array(name)[rows], terminator: "")
}
else{
print(" ", terminator: "" )
}
}
print("")
}
String st1 = "HURRICANE";
int a = 0;
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(5 - i));
} else {
System.out.print(' ');
}
}
System.out.println();
}
for (int i = 2; i <= 5; i++) {
for (int j = 1; j <= 9; j++) {
if (j == i || (10 - i) == j) {
System.out.print(st1.charAt(3 + i));
} else {
System.out.print(' ');
}
}
System.out.println();
}
Let's assume that a word has an odd number of characters, otherwise we get a crooked diamond.
Try it online!
public static void main(String[] args) {
String str = "abrahadabra";
int n = str.length() / 2;
for (int i = -n, ch = 0; i <= n && ch < str.length(); i++, ch++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) + Math.abs(j) == n)
System.out.print(str.charAt(ch));
else
System.out.print(" ");
System.out.println();
}
}
Output:
a
b b
r r
a a
h h
a a
d d
a a
b b
r r
a
I have this table (2d array) and I'm using a Random utility from numbers 0-4 to select a row and column to replace a number with the letter "P" and I have everything but I get this.
- Type mismatch: cannot convert from String
to int
- Type mismatch: cannot convert from
Random to int
- Type mismatch: cannot convert from
Random to int
Meanwhile this question How to change value of array element in 2D arrays? says you can just do
someArray[row][column] = "x";
Here is what I have (I assume my problem is with using the random)
import java.util.Random;
public class Server{
private int number;
private boolean pennyLanded;
public Server()
{
int n = 0;
number = n;
pennyLanded = false;
}
public boolean pennyLanded()
{
return pennyLanded;
}
public void setPennyLanded()
{
pennyLanded = true;
}
public int getNumber()
{
return number;
}
public String toString()
{
if (pennyLanded)
return "P";
else
return "" + number;
}
public static int[][] tableMaker(){
int[][] table = new int[5][5];
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(i==2 && j==2){
table[i][j] =3;
}
else if(i==0 || i==4){
table[i][j] = 1;
}
else if(j==4 || j==0){
table[i][j] = 1;
}
else if((i==1 || i==3) && (j>0 || j<4)){
table[i][j] = 2;
}
else if((i==2 && j==1) || (i==2 && j==3)){
table[i][j] = 2;
}
}
}
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
System.out.print(table[i][j] + " ");
}
System.out.println();
}
return table;
}
public static int[][] tossPenny(){
Random row = new Random();
Random column = new Random();
int[][] table = Server.tableMaker();
for(int i=0; i<5; i++){
table[row.nextInt(4)][column.nextInt(4)] = -1;
}
return table;
}
}
The table is printed in a Client class
public class Client{
public static void main(String[] args){
System.out.println(Server.tableMaker());
}
}
I Don't know how simple of a fix this is, and I have searched I just have a particular problem
The table is printed as so
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
And I would like something like this say the random puts out 1 and 2
1 1 1 1 1
1 2 P 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
You have to use row.nextInt(int max) instead of row, where the generated random integer n is 0 <= n < max. The parameter of new Random() only defines a seed, so I suggest you not use a constant number there unless you want your random algorithm to be deterministic.
Furthermore you mixes up the types a bit. tableMaker() should be String[][] in order to write Strings into it and tossPenny() should be void since it doesn't return anything.
import java.util.Random;
public class Server {
public static String[][] tableMaker() {
String[][] table = new String[5][5];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table.length; j++) {
if (i == 2 && j == 2) {
table[i][j] = "" + 3; // Converting int 3 to String "3"
} else if (i == 0 || i == 4) {
table[i][j] = "" + 1;
} else if (j == 4 || j == 0) {
table[i][j] = "" + 1;
} else if ((i == 1 || i == 3) && (j > 0 || j < 4)) {
table[i][j] = "" + 2;
} else if ((i == 2 && j == 1) || (i == 2 && j == 3)) {
table[i][j] = "" + 2;
}
}
}
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table.length; j++) {
System.out.print(table[i][j] + " ");
}
System.out.println();
}
return table;
}
public static void tossPenny(int a) {
Random row = new Random();
Random column = new Random();
String Penny = "P";
String[][] table = Server.tableMaker();
for (int i = 0; i < 5; i++) {
table[row.nextInt(5)][column.nextInt(5)] = Penny;
}
}
}
Was able to figure it out with help of a IRL friend and this avoids the memory store errors as seen before, Server is below
import java.util.Random;
public class Server{
private static int[][] table;
public static int[][] tableMaker(){
table = new int[5][5];
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(i==2 && j==2){
table[i][j] =3;
}
else if(i==0 || i==4){
table[i][j] = 1;
}
else if(j==4 || j==0){
table[i][j] = 1;
}
else if((i==1 || i==3) && (j>0 || j<4)){
table[i][j] = 2;
}
else if((i==2 && j==1) || (i==2 && j==3)){
table[i][j] = 2;
}
}
}
return table;
}
public static void printTable(){
for(int i=0; i<table.length; i++){
for(int j=0; j<table.length; j++){
if(table[i][j] == -1) System.out.print("P ");
else System.out.print(table[i][j] + " ");
}
System.out.println();
}
}
public static int[][] tossPenny(){
Random row = new Random();
Random column = new Random();
for(int i=0; i<5; i++){
table[row.nextInt(4)][column.nextInt(4)] = -1;
}
return table;
}
}
Client is here
import java.util.Scanner;
public class Client{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
Server.tableMaker();
Server.printTable();
System.out.println();
Server.tossPenny();
Server.printTable();
input.close();
}
}
Thanks a lot everyone especially #DavidWallace You helped me get around most of my problems and I was able to fix everything using the private static int[][] table;