Tic-tac-toe java: what to do if the user inputs a position that's already taken? - java

Here is the code I have written so far. My game works just fine but the problem I have is if the user inputs a position that's already taken, yes it keeps asking the user for input, but what i would want it to do it say: This position is unavailable and then reask the user until it's good. I thought about a for loop, but with the loop I have, it says it's unavailable even if it is not...I have been on this for days. If someone could give me some hints I would appreciate it.
import java.util.Scanner;
public class A3Q1_40011419 {
//Creating the board for the user input
static char [][] board = {{'1','2','3'}, {'4','5','6'},{'7','8','9'}};
static Scanner keyboard = new Scanner (System.in);
//Keeping track of the turns
public static int turns = 0;
//Drawing the board
public static void drawBoard() {
int row = 0;
int col = 0;
int turn = 0;
for (row = 0; row < board.length; row++){
for (col = 0; col < board[row].length; col++){
System.out.print(board[row][col]);
System.out.print(" ");
}
System.out.println();
}
}
//Asking user for input
public static void getInput (int playerNum) {
char piece;
char location = ' ';
if (playerNum != 1){
System.out.println("Player X - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'X';
}
else{
System.out.println("Player O - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'O';
}
if (location == '1')
if (board[0][0]=='1')
board[0][0] = piece;
else getInput(playerNum);
else if (location == '2')
if (board[0][1] == '2')
board[0][1] = piece;
else getInput(playerNum);
else if (location == '3')
if (board[0][2] == '3')
board[0][2] = piece;
else getInput(playerNum);
else if (location == '4')
if (board[1][0] == '4')
board[1][0] = piece;
else getInput(playerNum);
else if (location == '5')
if (board[1][1] == '5')
board[1][1] = piece;
else getInput(playerNum);
else if (location == '6')
if (board[1][2] == '6')
board[1][2] = piece;
else getInput(playerNum);
else if (location == '7')
if (board[2][0] == '7')
board[2][0] = piece;
else getInput(playerNum);
else if (location == '8')
if (board[2][1] == '8')
board[2][1] = piece;
else getInput(playerNum);
else if (location == '9')
if (board[2][2] == '9')
board[2][2] = piece;
else getInput(playerNum);
else if (location != '1' || location !='2' || location !='3' || location != '4' || location !='5' || location !='6' || location != '7' || location != '8'|| location !='9'){
System.out.println("That is not a valid position - must be between 1 and 9 inclusive.");
getInput(playerNum);
}
for (int i = 0; i<board.length; i++){
for (int j = 0; j<board.length; j++)
/*if (location == '1' || location == '2' || location == '3'|| location == '4' || location == '5'|| location == '6' || location == '7' || location == '8' || location == '9')*/
if (board[i][j] == 'X' || board[i][j] == 'O'){
System.out.println("Position not available"); //This is was i tried to do so far to check is the position was available on the board. Not working
}
getInput(playerNum);
}
}
//Checking if position available
public static void isAvailable(){ //This is a method i thought of creating to check if the position was available but I have not used it yet.
char location_1 = ' ';
char location_2 = ' ';
char location_3 = ' ';
char location_4 = ' ';
char location_5 = ' ';
char location_6 = ' ';
char location_7 = ' ';
char location_8 = ' ';
char location_9 = ' ';
}
/*Checking if there is a winner by checking if the cells in the board are equal to each other
*and checking if the cell isn't empty. If one isn't empty and they're all equal, then
none of them is empty.*/
public static char isWinner() {
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0]!='1')
return board[0][0];
else if (board[0][0]==board[0][1] && board[0][0]==board[0][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] !='3')
return board[0][2];
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != '3')
return board[0][2];
else if (board[2][0] == board [2][1] && board[2][0] == board[2][2] && board[2][0] != '7')
return board[2][0];
else if (board[1][0] == board [1][1] && board[1][0] == board[1][2] && board[1][0] != '4')
return board[1][0];
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != '2')
return board[0][1];
else
return 'Y';
}
//New board for a new game
public static void newBoard() {
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
//Main method
public static void main(String[] args) {
int game = 0;
int playerNum = 1;
String answer;
do {
turns = 0;
newBoard();
while (isWinner() == 'Y') {
drawBoard();
isWinner();
playerNum *= -1;
getInput(playerNum);
turns++;
System.out.println(turns);
if (turns == 9) {
break;
}
}
drawBoard();
if (isWinner() == 'X')
System.out.println("Player X wins");
else if (isWinner()=='O')
System.out.println("Player O wins");
else
System.out.println("It's a tie!");
System.out.println("Would you like to play another game?");
answer = keyboard.next().toLowerCase();
}
while (answer.equals("yes"));
}
}

I would suggest:
You move your check for available position before inserting the new input.
Instead of a loop to check all positions of the grid, you check only the position the player is asking.
Please find a working code below:
public class A3Q1_40011419 {
//Creating the board for the user input
static char [][] board = {{'1','2','3'}, {'4','5','6'},{'7','8','9'}};
static Scanner keyboard = new Scanner (System.in);
//Keeping track of the turns
public static int turns = 0;
//Drawing the board
public static void drawBoard() {
int row = 0;
int col = 0;
int turn = 0;
for (row = 0; row < board.length; row++){
for (col = 0; col < board[row].length; col++){
System.out.print(board[row][col]);
System.out.print(" ");
}
System.out.println();
}
}
//Asking user for input
public static void getInput (int playerNum) {
char piece;
char location = ' ';
if (playerNum != 1){
System.out.println("Player X - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'X';
}
else{
System.out.println("Player O - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'O';
}
//------------------------
// Your new check
int locationInt = Integer.parseInt("" + location);
int x = (locationInt-1)/3;
int y = ((locationInt-1)%3);
if (board[x][y] == 'X' || board[x][y] == 'O'){
System.out.println("Position not available"); //This is was i tried to do so far to check is the position was available on the board. Not working
getInput(playerNum);
return;
}
//------------------------
if (location == '1')
if (board[0][0]=='1')
board[0][0] = piece;
else getInput(playerNum);
else if (location == '2')
if (board[0][1] == '2')
board[0][1] = piece;
else getInput(playerNum);
else if (location == '3')
if (board[0][2] == '3')
board[0][2] = piece;
else getInput(playerNum);
else if (location == '4')
if (board[1][0] == '4')
board[1][0] = piece;
else getInput(playerNum);
else if (location == '5')
if (board[1][1] == '5')
board[1][1] = piece;
else getInput(playerNum);
else if (location == '6')
if (board[1][2] == '6')
board[1][2] = piece;
else getInput(playerNum);
else if (location == '7')
if (board[2][0] == '7')
board[2][0] = piece;
else getInput(playerNum);
else if (location == '8')
if (board[2][1] == '8')
board[2][1] = piece;
else getInput(playerNum);
else if (location == '9')
if (board[2][2] == '9')
board[2][2] = piece;
else getInput(playerNum);
else if (location != '1' || location !='2' || location !='3' || location != '4' || location !='5' || location !='6' || location != '7' || location != '8'|| location !='9'){
System.out.println("That is not a valid position - must be between 1 and 9 inclusive.");
getInput(playerNum);
}
}
//Checking if position available
public static void isAvailable(){ //This is a method i thought of creating to check if the position was available but I have not used it yet.
char location_1 = ' ';
char location_2 = ' ';
char location_3 = ' ';
char location_4 = ' ';
char location_5 = ' ';
char location_6 = ' ';
char location_7 = ' ';
char location_8 = ' ';
char location_9 = ' ';
}
/*Checking if there is a winner by checking if the cells in the board are equal to each other
*and checking if the cell isn't empty. If one isn't empty and they're all equal, then
none of them is empty.*/
public static char isWinner() {
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0]!='1')
return board[0][0];
else if (board[0][0]==board[0][1] && board[0][0]==board[0][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] !='3')
return board[0][2];
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != '3')
return board[0][2];
else if (board[2][0] == board [2][1] && board[2][0] == board[2][2] && board[2][0] != '7')
return board[2][0];
else if (board[1][0] == board [1][1] && board[1][0] == board[1][2] && board[1][0] != '4')
return board[1][0];
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != '2')
return board[0][1];
else
return 'Y';
}
//New board for a new game
public static void newBoard() {
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
//Main method
public static void main(String[] args) {
int game = 0;
int playerNum = 1;
String answer;
do {
turns = 0;
newBoard();
while (isWinner() == 'Y') {
drawBoard();
isWinner();
playerNum *= -1;
getInput(playerNum);
turns++;
System.out.println(turns);
if (turns == 9) {
break;
}
}
drawBoard();
if (isWinner() == 'X')
System.out.println("Player X wins");
else if (isWinner()=='O')
System.out.println("Player O wins");
else
System.out.println("It's a tie!");
System.out.println("Would you like to play another game?");
answer = keyboard.next().toLowerCase();
}
while (answer.equals("yes"));
}
}

Related

TicTacToe using 2D array in java

Hello Everyone I need help.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] board = {
{'?','?','?'},
{'?','?','?'},
{'?','?','?'}
};
System.out.print("Type any key to play the game and type 'n' to stop the game: ");
String Stop = sc.nextLine();
while(true){
if(Stop.equals("n"))break;
System.out.print("Player" + "[" + "X" + "]: ");
int PlayerX = sc.nextInt();
if(PlayerX == 1){
board[2][0] = 'x';
}
if(PlayerX == 2){
board[2][1] = 'x';
}
if(PlayerX == 3){
board[2][2] = 'x';
}
if(PlayerX == 4){
board[1][0] = 'x';
}
if(PlayerX == 5){
board[1][1] = 'x';
}
if(PlayerX == 6){
board[1][2] = 'x';
}
if(PlayerX == 7){
board[0][0] = 'x';
}
if(PlayerX == 8){
board[0][1] = 'x';
}
if(PlayerX == 9){
board[0][2] = 'x';
}
for(char[] x1 : board){
for(char x2 : x1){
System.out.print(x2 + "\t");
}
System.out.println();
}
System.out.print("Player" + "[" + "O" + "]: ");
int PlayerO = sc.nextInt();
if(PlayerO == 1){
board[2][0] = 'o';
}
if(PlayerO == 2){
board[2][1] = 'o';
}
if(PlayerO == 3){
board[2][2] = 'o';
}
if(PlayerO == 4){
board[1][0] = 'o';
}
if(PlayerO == 5){
board[1][1] = 'o';
}
if(PlayerO == 6){
board[1][2] = 'o';
}
if(PlayerO == 7){
board[0][0] = 'o';
}
if(PlayerO == 8){
board[0][1] = 'o';
}
if(PlayerO == 9){
board[0][2] = 'o';
}
for(char[] x1 : board){
for(char x2 : x1){
System.out.print(x2 + "\t");
}
System.out.println();
}
}
}
}
I am trying to make a simple tictactoes program in java. I am already done in placing the X and O, but I am struggling on checking of wether there is a winner or not.
I am confused on what code I will type o check the winner of the program.
You simply just need to write some code to check if there are 3 matches in each row column and diagonal.
You can utilise for loops to do this more efficiently
public static char checkWinner(char[][] board) {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] != '?' && board[i][0] == board[i][1] && board[i][1] == board[i][2]) {
return board[i][0];
}
}
// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] != '?' && board[0][j] == board[1][j] && board[1][j] == board[2][j]) {
return board[0][j];
}
}
// Check diagonal
if (board[0][0] != '?' && board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
return board[0][0];
}
// Check anti-diagonal
if (board[0][2] != '?' && board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
return board[0][2];
}
// No winner
return '?';
}
If you havent come accross for loops yet, there are plenty of good tutorials out there such as https://www.w3schools.com/java/java_for_loop.asp
(Note, you can also use loops to clean up some of your pre-existing code)

How can I keep it running after checking the if condition?

This is a program tic tac toe. It's a computer operation. I want it to check all conditions. If it meets the condition, it will check again if the position of the computer to work. Can add O or not if it can't add O. had the computer check the next condition completely, if it didn't meet the condition it would randomly add O, but now it doesn't work because it happens that sometimes the computer randomly added it in the first place. and then when the players Played the condition that the computer had to prevent the player from winning, but it had a random O In the previous round, it was unable to fill the O at all, thus preventing it from continuing.
private static void computerNormalTurn(char[][] board) {
Random rand = new Random();
int computerMove;
while (true) {
if(board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X'){
if(board[2][2] != ' ') {
continue;
}else{
computerMove = 3;
}
} else if (board [2][0] == 'X' && board [2][2] == 'X' ||
board [1][1] == 'X' && board [0][1] == 'X') {
if(board[2][1] != ' ') {
continue;
}else{
computerMove = 2;
}
} else if (board [2][1] == 'X' && board [2][2] == 'X' ||
board [1][0] == 'X' && board [0][0] == 'X' ||
board [1][1] == 'X' && board [0][2] == 'X' ) {
if(board[2][0] != ' ') {
continue;
}else{
computerMove = 1;
}
} else if (board[2][0] == 'X' && board[0][0] == 'X' ||
board [1][1] == 'X' && board [1][2] == 'X' ) {
if(board[1][0] != ' ') {
continue;
}else{
computerMove = 4;
}
} else if (board [2][0] == 'X' && board [0][2] == 'X' ||
board [2][1] == 'X' && board [0][1] == 'X' ||
board [2][2] == 'X' && board [0][0] == 'X' ||
board [1][0] == 'X' && board [1][2] == 'X' ) {
if(board[1][1] != ' ') {
continue;
}else{
computerMove = 5;
}
} else if (board[2][2] == 'X' && board[0][2] == 'X' ||
board [1][0] == 'X' && board [1][1] == 'X') {
if(board[1][2] != ' ') {
continue;
}else{
computerMove = 6;
}
} else if (board[2][0] == 'X' && board[1][0] == 'X' ||
board [2][2] == 'X' && board [1][1] == 'X' ||
board [0][1] == 'X' && board [0][2] == 'X') {
if(board[0][0] != ' ') {
continue;
}else{
computerMove = 7;
}
} else if (board [2][1] == 'X' && board [1][1] == 'X' ||
board [0][0] == 'X' && board [0][2] == 'X' ) {
if(board[0][1] != ' ') {
continue;
}else{
computerMove = 8;
}
} else if (board[2][0] == 'X' && board[1][1] == 'X' ||
board [2][2] == 'X' && board [1][2] == 'X' ||
board [0][0] == 'X' && board [0][1] == 'X' ) {
if(board[0][2] != ' ') {
continue;
}else{
computerMove = 9;
}
}else {
computerMove = rand.nextInt(9) + 1;
}
if (isValidMove(board, Integer.toString(computerMove))) {
break;
}
}
placeMove(board, Integer.toString(computerMove), 'O');
System.out.println("Computer chose " + computerMove);
}
I think the problem is that if your board is empty the computer will still have a move because of the else condition else { computerMove = rand.nextInt(9)+1; }
Then the following if statement will give a valid move and you will break the while loop and place the move on the board.
So if you want to prevent the computer from playing first, you should make a function that checks if the board is empty and if it is then do nothing.
public boolean isEmpty(){
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
if(board[i][j] != ' ') return false;
return true
}
If needed you can pass the board as a parameter in the function.
Hope I helped. Good luck!
There's a problem with the continues. The continue causes the next iteration. Because nothing changed, the game stays in the same state, the same condition will be met and the loop will continue endless.
Rewrite your conditions, so instead of e.g.
if(board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X'){
if(board[2][2] != ' ') {
continue;
}else{
computerMove = 3;
}
} else ...
write
if((board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X') &&
board [2][2] == ' '){
computerMove = 3;
} else ...

How can I shrink down all of these if statements?

This part of the code is from a school assignment. I got it to work, but I feel like I can simplify it or at least make it look cleaner. However, I have not yet been able to do so. Any suggestions? (It is from a tic tac toe game)
if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][0] != '-') {
winner = board[0][0];
} else if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][0] != '-') {
winner = board[1][0];
} else if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][0] != '-') {
winner = board[2][0];
} else if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[0][0] != '-') {
winner = board[0][0];
} else if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[0][1] != '-') {
winner = board[0][1];
} else if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[0][2] != '-') {
winner = board[0][2];
} else if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[2][0] != '-') {
winner = board[2][0];
} else if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-') {
winner = board[0][0];
}
try
if(check(board[0][0],board[0][1],board[0][2]) && board[0][2]!='-')
.....
private boolean check(a,b,c){
return a==b && b==c;
}
also you can see a better solution here
Here is another way of doing it:
int[][] checks = {{0,0,0,1},{1,0,0,1},{2,0,0,1}, // horizontals
{0,0,1,0},{0,1,1,0},{0,2,1,0}, // verticals
{0,0,1,1},{2,0,-1,1}}; // diagonals
char winner = '-';
for (int[] check : checks)
if ((winner = checkWinner(board, check[0], check[1], check[2], check[3])) != '-')
break;
private static char checkWinner(char[][] board, int y, int x, int dy, int dx) {
char c = board[y][x];
return (board[y + dy][x + dx] == c && board[y + dy * 2][x + dx * 2] == c ? c : '-');
}
What about following approach. As I can see, you have limited number of winners: board[0][0], board[1][0], board[2][0], board[0][1], board[2][0]. You can create separate Predicate for each winnger with appropriate name.
Predicate<char[][]> isZeroOneWinner = new Predicate<char[][]>() {
#Override
public boolean test(char[][] board) {
return board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[0][1] != '-';
}
};
I think is is better that multiple if...else.

Java 2-D Arrays - Tic Tac Toe Game

This tic tac toe game is user vs computer based.
For this assignment, I'm supposed to fill in the missing pieces, which is just finishing up the main and moveAI methods(the other methods are already preset, I just need to call them); there is comments provided where I need to add code to make this tic tac toe game work. I believe that my main method that I have is almost done except for the 2 empty lines under the comments, I'm not sure what they mean by that and also I'm currently stuck on getting my moveAI method to work. This is where I'm supposed to generate random places where the computer will move after I put an X somewhere. I'm not sure how to write something that will check to make sure the spot where the computer moves is available and not already taken by the user. Thanks in advance for the help!
import java.util.Random;
import java.util.Scanner;
public class TicTacToeGame
{
public static void main ( String[] args )
{
Scanner in = new Scanner ( System.in );
char[][] board = new char[ 3 ][ 3 ];
int x, y = -1;
char winner = 'N';
// Initialize the board to spaces
// boolean noWinner = true;
char player = 'X';
for (int r = 0; x < board.length(); r++)
{
for (int c = 0; c < board.length(); c++)
{
board[r][c] = ' ';
}
}
// Print the game board
printBoard(board);
// Keep playing while the game isn't finished
while (winner == 'N')
{
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
x = in.nextInt();
y = in.nextInt();
}
// Get the location from the user and validate it
// Mark the position in the board according to the user's specified location
// Print the board after the user plays
printBoard(board);
// Check to see if the game is finished. If it is, break out of the loop.
// Have the AI make a move
moveAI(board);
// Print the board after the AI plays
printBoard(board);
// Check to see who the winner is
winner = checkWinner(board);
}
// If the winner is 'X' or 'O', print that, otherwise, it is a tie
if (winner == 'X')
System.out.println("X is the winner!");
else if (winner == 'O')
System.out.println("O is the winner!");
else
System.out.println("Tie");
}
/**
* Makes a move for the AI, and marks the board with an 'O'.
*
* #param board The game board
*/
public static void moveAI ( char[][] board )
{
int x,y = -1;
Random r = new Random();
x = r.nextInt(3);
y = r.nextInt(3);
boolean open = false;
// Validate that the random location generated is valid.
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
while (open == false)
{
if (board[r][c] != ' ')
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
}
}
// Keep recalculating the location if the one generated is not
// if (board[r][c] != ' ')
//{
// an empty space.
System.out.print(" ");
// Be sure to mark the position in the board with an 'O'
board[][]=in.nextInt(3) + 'O';
}
/**
* Prints out the tic-tac-toe board
*
* #param board The game board
*/
public static void printBoard ( char[][] board )
{
// Box drawing unicode characters:
char a = '\u250c'; // U+250C : top-left
char b = '\u2510'; // U+2510 : top-right
char c = '\u2514'; // U+2514 : bottom-left
char d = '\u2518'; // U+2518 : bottom-right
char e = '\u252c'; // U+252C : top-vertical-connector
char f = '\u2534'; // U+2534 : bottom-vertical-connector
char g = '\u251c'; // U+251C : left-horizontal-connector
char h = '\u2524'; // U+2524 : right-horizontal-connector
char i = '\u253c'; // U+253C : center plus sign connector
char j = '\u2500'; // U+2500 : horizontal
char k = '\u2502'; // U+2502 : vertical
String l = j + "" + j + "" + j; // Three horizontals
// Print out the game board
System.out.printf ( "\n 0 1 2\n" +
" %c%s%c%s%c%s%c\n" +
"0 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"1 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"2 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n\n",
a, l, e, l, e, l, b,
k, board[0][0], k, board[0][1], k, board[0][2], k,
g, l, i, l, i, l, h,
k, board[1][0], k, board[1][1], k, board[1][2], k,
g, l, i, l, i, l, h,
k, board[2][0], k, board[2][1], k, board[2][2], k,
c, l, f, l, f, l, d );
}
/**
* Checks the result of the game
*
* #param board The game board
* #return 'X' if 'X' is the winner
* 'O' if 'O' is the winner
* 'T' if the game is a tie
* 'N' if the game isn't finished
*/
public static char checkWinner( char[][] board )
{
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || // Check row 0
board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || // Check row 1
board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || // Check row 2
board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || // Check col 0
board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || // Check col 1
board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || // Check col 2
board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || // Check diag \
board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || // Check diag /
board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' || // Check row 0
board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' || // Check row 1
board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' || // Check row 2
board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' || // Check col 0
board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' || // Check col 1
board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' || // Check col 2
board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' || // Check diag \
board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x') // Check diag /
{
return 'X';
}
else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // Check row 0
board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // Check row 1
board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // Check row 2
board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // Check col 0
board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // Check col 1
board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // Check col 2
board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // Check diag \
board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' || // Check diag /
board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o' || // Check row 0
board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o' || // Check row 1
board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o' || // Check row 2
board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o' || // Check col 0
board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o' || // Check col 1
board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o' || // Check col 2
board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o' || // Check diag \
board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o') // Check diag /
{
return 'O';
}
boolean finished = true;
// If there is a blank space in the board, the game isn't finished yet
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[ i ].length; j++)
if (board[ i ][ j ] == ' ')
finished = false;
// If the board is finished and 'X' or 'O' wasn't returned, then it is a tie
// Otherwise, the game is not finished yet
if ( finished )
return 'T';
else
return 'N';
}
}
// Get the location from the user and validate it
It looks like you are already doing this in the preceding lines:
// you're validating the input here
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
// you're getting the location from the user here
x = in.nextInt();
y = in.nextInt();
}
You probably still need to check to see if the position the user entered is a position that's already been filled in. You need to look in the array for that. There's a comment in moveAI giving you a hint for this. You check if board[x][y] is a space.
Also, I think you have a bug there because you never reset the values for x and y, so the next time the player's turn comes around the loop will not be entered. x and y still have the last turn's values so the loop condition evaluates to false. I think you can solve this problem by using a do{...}while(...); loop instead of a while(...){...} loop.
// Mark the position in the board according to the user's specified location
This just means put an 'O' in board[x][y].
// Check to see if the game is finished. If it is, break out of the loop.
It looks like checkWinner does this. It returns 'T' if the game is done and 'N' if it's still going.
For the AI code, here is some pseudocode for what I think you should be doing:
1. generate random x, y coordinates
2. check if board[x][y] is already used (there's a comment that tells how)
3. if it's used, go back to 1, otherwise put an 'X' in that space and return
Also, you don't need the loops that check for while (x < 0 && x >2 && y < 0 && y > 2) in the AI code. Random.nextInt(3) already returns a number in the range 0-2.

String to Char || Char to String

Let suppose I have a button
case R.id.button:
which will do the following functionality:
int position;
String keyInStringForm = et2.getText().toString();
int keyInIntegerForm = Integer.parseInt(keyInStringForm);
String text = et1.getText().toString();
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == 'a' || text.charAt(i) == 'A') {
position = 0;
break;
} else if (text.charAt(i) == 'b' || text.charAt(i) == 'B') {
position = 1;
break;
} else if (text.charAt(i) == 'c' || text.charAt(i) == 'C') {
position = 2;
break;
} else if (text.charAt(i) == 'd' || text.charAt(i) == 'D') {
position = 3;
break;
} else if (text.charAt(i) == 'e' || text.charAt(i) == 'E') {
position = 4;
break;
} else if (text.charAt(i) == 'f' || text.charAt(i) == 'F') {
position = 5;
break;
} else if (text.charAt(i) == 'g' || text.charAt(i) == 'G') {
position = 6;
break;
} else if (text.charAt(i) == 'h' || text.charAt(i) == 'H') {
position = 7;
break;
} else if (text.charAt(i) == 'i' || text.charAt(i) == 'I') {
position = 8;
break;
} else if (text.charAt(i) == 'j' || text.charAt(i) == 'J') {
position = 9;
break;
} else if (text.charAt(i) == 'k' || text.charAt(i) == 'K') {
position = 10;
break;
} else if (text.charAt(i) == 'l' || text.charAt(i) == 'L') {
position = 11;
break;
} else if (text.charAt(i) == 'm' || text.charAt(i) == 'M') {
position = 12;
break;
} else if (text.charAt(i) == 'n' || text.charAt(i) == 'N') {
position = 13;
break;
} else if (text.charAt(i) == 'o' || text.charAt(i) == 'O') {
position = 14;
break;
} else if (text.charAt(i) == 'p' || text.charAt(i) == 'P') {
position = 15;
break;
} else if (text.charAt(i) == 'q' || text.charAt(i) == 'Q') {
position = 16;
break;
} else if (text.charAt(i) == 'r' || text.charAt(i) == 'R') {
position = 17;
break;
} else if (text.charAt(i) == 's' || text.charAt(i) == 'S') {
position = 18;
break;
} else if (text.charAt(i) == 't' || text.charAt(i) == 'T') {
position = 19;
break;
} else if (text.charAt(i) == 'u' || text.charAt(i) == 'U') {
position = 20;
break;
} else if (text.charAt(i) == 'v' || text.charAt(i) == 'V') {
position = 21;
break;
} else if (text.charAt(i) == 'w' || text.charAt(i) == 'W') {
position = 22;
break;
} else if (text.charAt(i) == 'x' || text.charAt(i) == 'X') {
position = 23;
break;
} else if (text.charAt(i) == 'y' || text.charAt(i) == 'Y') {
position = 24;
break;
} else if (text.charAt(i) == 'z' || text.charAt(i) == 'Z') {
position = 25;
break;
} else if (text.charAt(i) == ' ') {
position = 26;
break;
}
int initialResult = position + keyInIntegerForm;
int finalResult = initialResult % 26;
char resultantChar = alphabets[finalResult];
where as "alphabets" is a char array for a-z characters.
} // for
Now there will be more that one "resultantChar", I want those "resultantChar" to be combined together to form a string so I can set it onto a textview.
How do I do that
If I understood you correctly, try do something like this:
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
...
char resultantChar = alphabets[finalResult];
result.append(resultantChar);
}
System.out.println(result);
Please, simplify your code using that!
char ch = 'Z';
ch = Character.toLowerCase(ch);
int position = Character.getNumericValue(ch) - Character.getNumericValue('a');
Or, for your case:
char ch = Character.toLowerCase(text.charAt(i));
if (ch >= 'a' && ch <= 'z') {
position = Character.getNumericValue(ch) - Character.getNumericValue('a');
} else if (ch == ' ') {
position = 26;
}
Use http://developer.android.com/reference/java/lang/StringBuilder.html stringbuilder you can append char with stringbuilder

Categories