java - Connect 4 using the board -
java - Connect 4 using the board -
we're programming little connect 4 game without gui. have problems using board fill in chips ('x's or 'o's). made char array draw board told in instructions , clueless how fill board chips. here's code:
char player = 'x'; char[][] board = new char[7][8]; public void sboard() { char[][] board = new char[7][8]; (int i=0;i<board.length-1;i++) { system.out.print("|"); (int j=0;j<board[i].length-1;j++) { board[i][j]='.'; system.out.print(board[i][j]+"|"); } system.out.println(); } }
this creates board , dots on it, player 1 , 2 fill either x or o.
public void userinput() { system.out.print("which coloumn? (1-7): "); scanner in = new scanner(system.in); int input = in.nextint(); in.close(); (int = feld.length; >0; i--) { if (board[i][ein-1] == '.') { if (player == 'x') { board[board.length - 2][ein - 1] = 'x'; system.out.print(board[board.length-2][input-1]); } if (player == 'o') { board[board.length - 2][ein - 1] = 'o'; system.out.print(input[board.length-2][input-1]); } if (player == 'o') player = 'x'; else player = 'o'; } system.out.println();
now since char array filled in
spielfeld, method, array empty when want fill x or o. there way have whole 2d array (values) copied beingabe method? if there's workaround/other way solve i'd appreciate it.
edit: translated variables in english. give vague thought does: "throws" x or o(beginning x) user inputed coloumns ranging 1-7
there aspects notice:
you define/overload variable board in 2 scopes, 1 global , 1 local.one alternative set global 1 in local method:
char[][] board = new char[7][8]; public void sboard() { board = new char[7][8]; ...
another alternative homecoming by
public char[][] board() { ... homecoming board; }
and utilize by
void beingabe(char[][] board) {...}
don't close system.in early, if want read 1 time again after, can't access anymore.
in.close();
better close it, when game's over, i.e. @ end of main()
you translated not enough, code doesnt compile in current form.as assignment school, it's fine implement this, learning java basics imo.
edit(sorry many, hard format on mobile):
and seems you're high german student, maybe teach little bits scopes: http://www.scalingbits.com/java/javakurs1/methoden/variablen-sichtbarkeit-lokal-global
java arrays
Comments
Post a Comment