//-------------------------------------//
// Isaac Johnson - Noughts and Crosses //
//-------------------------------------//
import java.util.Scanner;
import java.util.Random;
class noughtsAndCrosses {
public static void main(String args[]){
//Game title
System.out.println("Noughts And Crosses\n");
//Declaring all objects and integers to be used in the game
boolean gameOver = false;
Scanner keyboard = new Scanner(System.in);
int gameCount = 0;
char[][] gameBoard = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
gameStart(keyboard);
// Main game methods inside the game loop
while (!gameOver){
gameCount++;
display(gameBoard);
userInput(keyboard, gameBoard);
//This if statement checks to see if the game is a draw
if (gameCount == 5)
{
gameOver = checkWin(gameBoard);
if (!gameOver)
{
System.out.println("The game ends in a draw!");
break;
}
else
break;
}
computerInput(gameBoard, keyboard);
gameOver = checkWin(gameBoard);
}
display(gameBoard);
}
//Method for displaying the game board, which is a two dimensional array using characters
public static void display(char[][] a){
for (int row = 0; row < a.length; row++){
for (int column = 0; column < a[row].length; column++){
System.out.print(a[row][column] + "\t");
}
System.out.println("\n\n");
}
}
//This method gets the players name and gives the player the first move
public static void gameStart(Scanner keyboard){
System.out.println("Please enter your first name: ");
String name = keyboard.next();
System.out.println("\nThanks, " + name + " it's your turn, choose a number to place an X!\n");
}
//Method for getting the users move
public static void userInput(Scanner keyboard, char[][] gameBoard){
int playerMove = 0;
char playerPiece = 'x';
boolean validMove = false;
while (validMove == false){
playerMove = keyboard.nextInt();
if (playerMove < 1 || playerMove > 9)
System.out.print("Invalid number, try again");
else
validMove = true;
}
placeMove(gameBoard, playerMove, playerPiece, keyboard);
}
//Method to place corresponding move on the game board, works for the player and computer moves
public static void placeMove(char[][] gameBoard, int move, char movePiece, Scanner keyboard){
switch (move) {
case 1:
if(gameBoard[0][0] != 'x' && gameBoard[0][0] != 'o')
{
gameBoard[0][0] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 2:
if(gameBoard[0][1] != 'x' && gameBoard[0][1] != 'o')
{
gameBoard[0][1] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 3:
if(gameBoard[0][2] != 'x' && gameBoard[0][2] != 'o')
{
gameBoard[0][2] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 4:
if(gameBoard[1][0] != 'x' && gameBoard[1][0] != 'o')
{
gameBoard[1][0] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 5:
if(gameBoard[1][1] != 'x' && gameBoard[1][1] != 'o')
{
gameBoard[1][1] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 6:
if(gameBoard[1][2] != 'x' && gameBoard[1][2] != 'o')
{
gameBoard[1][2] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 7:
if(gameBoard[2][0] != 'x' && gameBoard[2][0] != 'o')
{
gameBoard[2][0] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 8:
if(gameBoard[2][1] != 'x' && gameBoard[2][1] != 'o')
{
gameBoard[2][1] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
case 9:
if(gameBoard[2][2] != 'x' && gameBoard[2][2] != 'o')
{
gameBoard[2][2] = movePiece;
}
else
{
System.out.print("Invalid move " + movePiece + ", try again\n");
if(movePiece == 'o')
computerInput(gameBoard, keyboard);
else
userInput(keyboard, gameBoard);
}
break;
}
}
//This method generates the computers move, which is based off a random number
public static void computerInput(char[][] gameBoard, Scanner keyboard){
Random rand = new Random();
char computerPiece = 'o';
int computerMove = 1+rand.nextInt(9);
System.out.println("The computer's move was " + computerMove + "\n");
placeMove(gameBoard, computerMove, computerPiece, keyboard);
}
//The check win method that checks to see if a player has one based on piece combinations
public static boolean checkWin(char[][] gameBoard){
boolean result = false;
for (int i = 0; i < gameBoard.length; i++)
if(gameBoard[i][0] == 'x' && gameBoard[i][1] == 'x' && gameBoard[i][2] == 'x' ||
gameBoard[0][i] == 'x' && gameBoard[1][i] == 'x' && gameBoard[2][i] == 'x' ||
gameBoard[0][0] == 'x' && gameBoard[1][1] == 'x' && gameBoard[2][2] == 'x' ||
gameBoard[0][2] == 'x' && gameBoard[1][1] == 'x' && gameBoard[2][0] == 'x')
{
System.out.println("Congratulations you win!\n");
result = true;
break;
}
for (int i = 0; i < gameBoard.length; i++)
if(gameBoard[i][0] == 'o' && gameBoard[i][1] == 'o' && gameBoard[i][2] == 'o' ||
gameBoard[0][i] == 'o' && gameBoard[1][i] == 'o' && gameBoard[2][i] == 'o' ||
gameBoard[0][0] == 'o' && gameBoard[1][1] == 'o' && gameBoard[2][2] == 'o' ||
gameBoard[0][2] == 'o' && gameBoard[1][1] == 'o' && gameBoard[2][0] == 'o')
{
System.out.println("You lose!\n");
result = true;
break;
}
return result;
}
}