March 02, 2020

Tik Toc Toe Game single player with computer Java code

Tik Tok Toe a computer game developed in java by I T Section Kozhikode

/**
 * Happy Birthday bestie !!!
 *
 */
package com.konzern.games;

import java.util.Scanner;

/**
 * @author konzernites
 *
 */
public class TikTocToe {

private int MATRIX_SIZE = 3;
private char[][] matrix = { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
private int moveCount = 1;
private char USER_MOVE = 'X';
private char COMP_MOVE = 'O';
private ComputerMove computerMove = null;

public TikTocToe() {
intialize();
}

private void intialize() {
computerMove = new ComputerMove(USER_MOVE,COMP_MOVE);
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(" **** INSTRUCTION **** ");
System.out.println(" User has to enter row and column ");
System.out.println(" Postion starts from (0,0) to (2,2) ");
TikTocToe t = new TikTocToe();
t.startGame();
System.out.println(" **** GAME OVER ***** ");
System.out.println(" Developed by consumerfed I T section kozhikode ");

}

private void startGame() {
// TODO Auto-generated method stub
char move = 'x';
Scanner scanner = new Scanner(System.in);
int rowMoved = 0;
int colMoved = 0;
while (moveCount < 9) {

if (moveCount % 2 == 0) {

matrix = computerMove.computeMove(matrix, moveCount, rowMoved, colMoved);
System.out.println("** COMPUTER MOVE **");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} else {

System.out.println("** YOUR MOVE **");
System.out.println(" Enter the row (values from 0 to 2): ");
int row = scanner.nextInt();
System.out.println(" Enter the col (values from 0 to 2): ");
int col = scanner.nextInt();
matrix[row][col] = USER_MOVE;
rowMoved =row;
colMoved= col;
}
printMatrix();
boolean winner = checkWinner(matrix, move);
if (winner) {
System.out.println("*** Congratulation *** ");
System.out.println(move + " is the winner ");
break;
}
moveCount++;
}

}

private void printMatrix() {
// TODO Auto-generated method stub

for (int row = 0; row < MATRIX_SIZE; row++) {
for (int col = 0; col < MATRIX_SIZE; col++) {
System.out.print(matrix[row][col] + " | ");
}
System.out.println("");
}

}



private boolean checkWinner(char[][] matrix, char move) {

if (move == matrix[0][0] && move == matrix[0][1] && move == matrix[0][2]) {
return true;
} else if (move == matrix[1][0] && move == matrix[1][1] && move == matrix[1][2]) {
return true;
} else if (move == matrix[2][0] && move == matrix[2][1] && move == matrix[2][2]) {
return true;
} else if (move == matrix[0][0] && move == matrix[1][0] && move == matrix[2][0]) {
return true;
} else if (move == matrix[0][1] && move == matrix[1][1] && move == matrix[2][1]) {
return true;
} else if (move == matrix[0][2] && move == matrix[1][2] && move == matrix[2][2]) {
return true;
} else if (move == matrix[0][0] && move == matrix[1][1] && move == matrix[2][2]) {
return true;
} else if (move == matrix[0][2] && move == matrix[1][1] && move == matrix[2][0]) {
return true;
} else {
return false;
}
}
}


/**
 * Computer Moves for user move in tik toc toe
 * Developed by consumerfed kozhikode I T section
 * Version 1.0 
 * consfedkozhikode@gmail.com
 * 
 */
package com.konzern.games;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

/**
 * @author konzernites
 *
 */
public class ComputerMove {

private char userMove = '\0';
private char compMove = '\0';
private int MATRIX_SIZE = 3;
private char EMPTY = ' ';
private Map<String, Integer> priorityPosition = null;


public ComputerMove(char userMove, char compMove) {
this.compMove = compMove;
this.userMove = userMove;

}



public char[][] computeMove(char[][] matrix, int moveCount, int rowMoved, int colMoved) {

if (moveCount <= 2) {
matrix = generateFirstCompMove(matrix);
} else {
matrix = generateCompMove(matrix, moveCount, rowMoved, colMoved);
}
return matrix;
}

private char[][] generateCompMove(char[][] matrix, int moveCount, int rowMoved, int colMoved) {
priorityPosition = new HashMap<>();
boolean isInserted = false;
for (int row = 0; row < MATRIX_SIZE; row++) {
for (int col = 0; col < MATRIX_SIZE; col++) {

if (EMPTY == matrix[row][col]) {
isInserted = findPriority(matrix, row, col);
if (isInserted)
return matrix;
}

}
}

if (!isInserted) {
// set max value of map to matrix
String key = maxUsingCollectionsMaxAndLambda(priorityPosition);
int r = Integer.parseInt(key.substring(0, 1));
int c = Integer.parseInt(key.substring(1, 2));
matrix[r][c] = compMove;
}
return matrix;
}

private boolean findPriority(char[][] matrix, int row, int col) {

boolean isInserted = true;
if (diagonalOne(matrix, 0, 0)) {
isInserted = true;
} else if (diagonalTwo(matrix, 0, MATRIX_SIZE - 1)) {
isInserted = true;
} else if (rowCheck(matrix, row, col)) {
matrix[row][col] = compMove;
} else if (colCheck(matrix, row, col)) {
matrix[row][col] = compMove;
} else {
isInserted = false;
}
return isInserted;
}

private boolean diagonalTwo(char[][] matrix, int row, int col) {
boolean isPrior = false;
boolean isSafe = false;
int loopCount = 1;
int value = 0;

int r = 0;
int c = 0;
while (loopCount <= MATRIX_SIZE) {

if (compMove == matrix[row][col]) {
isSafe = true;
break;
} else if (userMove == matrix[row][col]) {
value++;
}
if (EMPTY == matrix[row][col]) {
r = row;
c = col;
}
row++;
col--;
loopCount++;
}

if (!isSafe) {
String key = String.valueOf(r + "" + c);
priorityPosition.put(key, value);
}

if (value == 2) {
matrix[r][c] = compMove;
isPrior = true;
}
return isPrior;
}

private boolean diagonalOne(char[][] matrix, int row, int col) {
boolean isPrior = false;
boolean isSafe = false;
int loopCount = 1;
int value = 0;
int r = 0;
int c = 0;
while (loopCount <= MATRIX_SIZE) {

if (compMove == matrix[row][col]) {
isSafe = true;
break;
} else if (userMove == matrix[row][col]) {
value++;
}
if (EMPTY == matrix[row][col]) {
r = row;
c = col;
}
row++;
col++;
loopCount++;
}

if (!isSafe) {
String key = String.valueOf(r + "" + c);
priorityPosition.put(key, value);
}

if (value == 2) {
matrix[r][c] = compMove;
isPrior = true;
}
return isPrior;
}

private boolean colCheck(char[][] matrix, int row, int col) {
boolean isPrior = false;
boolean isSafe = false;
int loopCount = 1;
int value = 0;

int r = row;
int c = col;
while (loopCount < MATRIX_SIZE) {
if (r + 1 == MATRIX_SIZE) {
r = 0;
} else {
r++;
}
if (compMove == matrix[r][c]) {
isSafe = true;
break;
} else if (userMove == matrix[r][c]) {
value++;
}
loopCount++;
}

if (!isSafe) {
String key = String.valueOf(row +""+ col);
priorityPosition.put(key, value);
}

if (value == 2) {
isPrior = true;
}
return isPrior;
}

private boolean rowCheck(char[][] matrix, int row, int col) {
boolean isPrior = false;
boolean isSafe = false;
int loopCount = 1;
int value = 0;

int r = row;
int c = col;
while (loopCount < MATRIX_SIZE) {

c = (c + 1 == MATRIX_SIZE) ? 0 : c + 1;

if (compMove == matrix[r][c]) {
isSafe = true;
break;
} else if (userMove == matrix[r][c]) {
value++;
}
loopCount++;
}

if (!isSafe) {
String key = String.valueOf(row + "" + col);
priorityPosition.put(key, value);
}

if (value == 2) {
isPrior = true;
}
return isPrior;
}

private char[][] generateFirstCompMove(char[][] matrix) {
int row = 0;
int col = 0;
if (EMPTY == matrix[1][1]) {
matrix[1][1] = compMove;
} else {
Random r = new Random();
row = r.nextInt(3);
col = r.nextInt(3);
col = (row == 1 && col == 1) ? col + 1 : col;
matrix[row][col] = compMove;
}
return matrix;
}

private <K, V extends Comparable<V>> K maxUsingCollectionsMaxAndLambda(Map<K, V> map) {
Entry<K, V> maxEntry = Collections.max(map.entrySet(),
(Entry<K, V> e1, Entry<K, V> e2) -> e1.getValue().compareTo(e2.getValue()));
return maxEntry.getKey();
}



private void printMatrix(char[][] matrix) {

for (int row = 0; row < MATRIX_SIZE; row++) {
for (int col = 0; col < MATRIX_SIZE; col++) {
System.out.print(matrix[row][col] + " | ");
}
System.out.println("");
}

}
}

Explanation on this code will be posted on the next post, also some update on the code is made and will be posted soon...



Please provide your feedback....


No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments