I am programming a tic tac toe game using 2 dimensional arrays, while/for loops,
ID: 645304 • Letter: I
Question
I am programming a tic tac toe game using 2 dimensional arrays, while/for loops, etc and I am having some issue on returning my new array once a player has entered the scanner values as coordinates into the console. The array should then have those input coordinates corresponding to an index in the matrix and replace '?' with an 'X' or 'O' depending on whos turn it is and this process continues (loops) until someone has won!!! Here is my code that I have written so far, plase feel free to copy and paste it and play around with it to see what I am doing wrong. THANKS!!!
//OUTPUT ON CMD PROMPT:
Shanes-iMac:desktop shanemoore$ javac TicTacToe.java
Shanes-iMac:desktop shanemoore$ java TicTacToe
|0|1|2|3|
----------
0 |?|?|?|?|
1 |?|?|?|?|
2 |?|?|?|?|
3 |?|?|?|?|
X it is your turn!
enter the coordinates of untaken cell in a row colum format
ie. 1 0 will place your mark in row 1 column 0 of the grid
your entry please...
1
0
|0|1|2|3|
----------
0 |?|?|?|?|
1 |?|?|?|?|
2 |?|?|?|?|
3 |?|?|?|?|
Shanes-iMac:desktop shanemoore$
//CODE:
import java.io.*;
import java.util.*;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
int n = 4;
char[][] gameBoard = new char[n][n];
Scanner s = new Scanner(System.in);
Random r = new Random(System.currentTimeMillis());
playGame(gameBoard, s, r);
}
/*
PURPOSE: to play a game of TicTacToe taking input from 2 players INPUT: char[][] board, a matrix holding tokens:
? indicates a blank space
O indicates a space claimed by X player X indicates a space claimed by O player
Scanner s, to provide command line input from the 2 players
Random r, a random number generator to decide who goes first OUTPUT: the board will be printed initially with all cells
containing
Explanation / Answer
import java.io.*;
import java.util.*;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
int n = 4;
char[][] gameBoard = new char[n][n];
Scanner s = new Scanner(System.in);
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
gameBoard[i][j] = '?';
}
}
Random r = new Random(System.currentTimeMillis());
playGame(gameBoard, s, r);
}
/*
* PURPOSE: to play a game of TicTacToe taking input from 2 players INPUT:
* char[][] board, a matrix holding tokens: ? indicates a blank space O
* indicates a space claimed by X player X indicates a space claimed by O
* player Scanner s, to provide command line input from the 2 players Random
* r, a random number generator to decide who goes first OUTPUT: the board
* will be printed initially with all cells containing
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.