Basic Java- Having trouble diplaying an array from a seperate method in main -
*edited- fixed code, off base of operations original. programme have now. it's intended similar connect 4 game instead of winning connecting diagonally, horizontally or vertically. way win connecting 4 xs vertically. i'm having problem figuring out code win. ideas?
package stackemupnew;
import java.util.scanner;
public class stackemupnew {
public static void main(string[] args) { //declares game space array char[][] gamespace = new char[6][6]; int[] rowtrack = new int[]{5, 5, 5, 5, 5, 5}; //prints number of columns above board (int c = 0; c < 6; c++) { system.out.print(c + " "); } system.out.println(" "); //sets game space it's starting state (int = 0; < gamespace.length; i++) { (int j = 0; j < gamespace[i].length; j++) { gamespace[i][j] = '.'; } } (int = 0; < gamespace.length; i++) { (int j = 0; j < gamespace[i].length; j++) { system.out.print(gamespace[i][j] + " "); } system.out.println(""); } //prints number of columns under board (int c = 0; c < 6; c++) { system.out.print(c + " "); } system.out.println(" "); while (windetector(gamespace) == false) { //run method lets user create move usermove(gamespace, rowtrack); (int c = 0; c < 6; c++) { system.out.print(c + " "); } system.out.println(" "); (int = 0; < gamespace.length; i++) { (int j = 0; j < gamespace[i].length; j++) { system.out.print(gamespace[i][j] + " "); } system.out.println(""); } (int c = 0; c < 6; c++) { system.out.print(c + " "); } system.out.println(" "); //run method checks win windetector(gamespace); //run method makes computer move computermove(gamespace, rowtrack); system.out.println("the computer's move is:"); (int c = 0; c < 6; c++) { system.out.print(c + " "); } system.out.println(" "); (int = 0; < gamespace.length; i++) { (int j = 0; j < gamespace[i].length; j++) { system.out.print(gamespace[i][j] + " "); } system.out.println(""); } (int c = 0; c < 6; c++) { system.out.print(c + " "); } system.out.println(" "); } windetector(gamespace); } //detects win not finished public static boolean windetector(char[][] gamespace){ boolean win = false; (int = 0; < gamespace.length; i++) { (int j = 0; j < gamespace[i].length; j++){ if (gamespace[i][j]=='x'&& gamespace[i][j+1]=='x'&& gamespace[i][j+2]=='x'&& gamespace[i][j+3]=='x'){ system.out.println("congratulations! win!"); win = true; } } } homecoming win; } public static void usermove(char[][] gamespace, int[] rowtrack) { scanner input = new scanner(system.in); system.out.print("where drop x?"); int col = input.nextint(); int row = rowtrack[col]; rowtrack[col]--; gamespace[row][col] = 'x'; } public static void computermove(char[][] gamespace, int[] rowtrack) { int col = (int) (math.random() * 6); int row = rowtrack[col]; rowtrack[col]--; gamespace[row][col] = 'o'; } }
output reads: 0 1 2 3 4 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0 1 2 3 4 5 exception in thread "main" java.lang.arrayindexoutofboundsexception: 6 @ stackemupnew.stackemupnew.windetector(stackemupnew.java:107) @ stackemupnew.stackemupnew.main(stackemupnew.java:48) java result: 1 build successful (total time: 0 seconds)
the method gamespaceinitial(int i,int j) accepts 2 int type parameters. forgetting pass them.
you need like,
system.out.println(gamespaceinitial(2,3));
java arrays methods main
No comments:
Post a Comment