Tik tok toe code explanation ( game developed in java )
isSpaceEmpty : is a method used to check whether the value of matrix is the specified rows and col is empty or not, If it is not an empty one then it will through an Illegal argument exception.
private boolean isSpaceEmpty(char[][] matrix, int row, int col) {
if (userMove == matrix[row][col] || compMove == matrix[row][col]) {
throw new IllegalArgumentException(techIssue);
}
return true;
}
generateFirstCompMove : The following function will calculate the first move. Computer will chose the centre of the matrix if not empty in other case random position generated.
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;
}
Happy Birthday Madhav my best friend... I miss you a lot
I was greatly inspired by madhav who has developed a paranoid game using C language when he was in higher secondary. It was year 2001 if i remember correctly, in that days computer in home where rare, study materials was not much abundantly available as now. So i dedicate this code for him.
No comments:
Post a Comment
Your feedback may help others !!!