Tic tac toe
/*File name:assiganmentTangWingSze1A.java
programmer: Tang Wing Sze
Description: assiganmentTangWingSze1A
*/
import java.util.Scanner;
public class assiganmentTangWingSze1A {
public static void main(String[] args) {
char board[][] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};//drawaboard
for (int i = 0; i < 3; i ++) {
System.out.println(" " + board[i][0] + " | " + board[i][1] + " | " + board[i][2]);
printline(i);//print---+---+---
}
int player;//for determine position
char XorO;//for printing 'X'/'O'
int markone = 0; //to determine whose turn or end game
while (markone != 9) {
XorO = determineplayer(markone);//determine playerX/O
player = inputplayer(XorO);//input position
if (player > 9 || player < 1 || board[(player - 1)/3][(player-1)%3] == 'X' || board[(player - 1)/3][(player-1)%3] == 'O') {
cheakerror(player);
continue;
}
// boardafterinput(markone, player);
for (int i = 0; i < 3; i ++) {
board[(player - 1)/3][(player-1)%3] = XorO;
System.out.println(" " + board[i][0] + " | " + board[i][1] + " | " + board[i][2]);//draw a board
printline(i);//print---+---+---
}
if (determinewinner(board,XorO)==1){ //determine winner.
return;
}
markone += 1;
}
System.out.println("The game is drawn!");//end game
}
public static void printline(int i) {
if (i < 2) {
System.out.println("---+---+---");
}
}
public static char determineplayer(int markone) {
Scanner input = new Scanner(System.in);
char XorO = (markone % 2 == 0) ? 'X' : 'O';//determine playerx/o
return XorO;
}
public static int inputplayer(char XorO) {
Scanner input = new Scanner(System.in);
System.out.print("Player " + XorO + " input the position(1-9): ");//input position
int player = input.nextInt();
return player;
}
public static void cheakerror(int player) {
System.out.println((player > 9 || player < 1) ? "The input range is 1-9!" : "The position is occupied!");
//input out of range or the position is occupied
}
public static int determinewinner(char [][]board ,char XorO){
int i=0;
if ( //horizotal
(board[0][0] == XorO && board[0][1] == XorO && board[0][2] == XorO)
|| (board[1][0] == XorO && board[1][1] == XorO && board[1][2] == XorO)
|| (board[2][0] == XorO && board[2][1] == XorO && board[2][2] == XorO)
//vertical
|| (board[0][0] == XorO && board[1][0] == XorO && board[2][0] == XorO)
|| (board[0][1] == XorO && board[1][1] == XorO && board[2][1] == XorO)
|| (board[0][2] == XorO && board[1][2] == XorO && board[2][2] == XorO)
//cross
|| (board[0][0] == XorO && board[1][1] == XorO && board[2][2] == XorO)
|| (board[0][2] == XorO && board[1][1] == XorO && board[2][0] == XorO)) {
System.out.println("The winner is " + XorO + "!");
i++;
}
return i;
}
}
programmer: Tang Wing Sze
Description: assiganmentTangWingSze1A
*/
import java.util.Scanner;
public class assiganmentTangWingSze1A {
public static void main(String[] args) {
char board[][] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};//drawaboard
for (int i = 0; i < 3; i ++) {
System.out.println(" " + board[i][0] + " | " + board[i][1] + " | " + board[i][2]);
printline(i);//print---+---+---
}
int player;//for determine position
char XorO;//for printing 'X'/'O'
int markone = 0; //to determine whose turn or end game
while (markone != 9) {
XorO = determineplayer(markone);//determine playerX/O
player = inputplayer(XorO);//input position
if (player > 9 || player < 1 || board[(player - 1)/3][(player-1)%3] == 'X' || board[(player - 1)/3][(player-1)%3] == 'O') {
cheakerror(player);
continue;
}
// boardafterinput(markone, player);
for (int i = 0; i < 3; i ++) {
board[(player - 1)/3][(player-1)%3] = XorO;
System.out.println(" " + board[i][0] + " | " + board[i][1] + " | " + board[i][2]);//draw a board
printline(i);//print---+---+---
}
if (determinewinner(board,XorO)==1){ //determine winner.
return;
}
markone += 1;
}
System.out.println("The game is drawn!");//end game
}
public static void printline(int i) {
if (i < 2) {
System.out.println("---+---+---");
}
}
public static char determineplayer(int markone) {
Scanner input = new Scanner(System.in);
char XorO = (markone % 2 == 0) ? 'X' : 'O';//determine playerx/o
return XorO;
}
public static int inputplayer(char XorO) {
Scanner input = new Scanner(System.in);
System.out.print("Player " + XorO + " input the position(1-9): ");//input position
int player = input.nextInt();
return player;
}
public static void cheakerror(int player) {
System.out.println((player > 9 || player < 1) ? "The input range is 1-9!" : "The position is occupied!");
//input out of range or the position is occupied
}
public static int determinewinner(char [][]board ,char XorO){
int i=0;
if ( //horizotal
(board[0][0] == XorO && board[0][1] == XorO && board[0][2] == XorO)
|| (board[1][0] == XorO && board[1][1] == XorO && board[1][2] == XorO)
|| (board[2][0] == XorO && board[2][1] == XorO && board[2][2] == XorO)
//vertical
|| (board[0][0] == XorO && board[1][0] == XorO && board[2][0] == XorO)
|| (board[0][1] == XorO && board[1][1] == XorO && board[2][1] == XorO)
|| (board[0][2] == XorO && board[1][2] == XorO && board[2][2] == XorO)
//cross
|| (board[0][0] == XorO && board[1][1] == XorO && board[2][2] == XorO)
|| (board[0][2] == XorO && board[1][1] == XorO && board[2][0] == XorO)) {
System.out.println("The winner is " + XorO + "!");
i++;
}
return i;
}
}