Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class TicTacToe {
- public static char[][] gameArea;
- public static Scanner input = new Scanner(System.in);
- public static int width = 0;
- public static int height = 0;
- public static int marksToWin = 0;
- public static void main(String[] args) {
- iniArea();
- printArea();
- do {
- askPlayer();
- checkVictory();
- compRandom();
- checkVictory();
- printArea();
- } while (true);
- }
- public static void iniArea() {
- boolean ok = true;
- do {
- try {
- System.out.println("Give the width of the area:");
- width = input.nextInt();
- } catch (Exception e) {
- System.out.println("Inappropriate value!");
- input.nextLine();
- }
- try {
- System.out.println("Give the height of the area:");
- height = input.nextInt();
- } catch (Exception f) {
- System.out.println("Inappropriate value!");
- input.nextLine();
- }
- try {
- System.out
- .println("Give the amount of consecutive marks to win:");
- marksToWin = input.nextInt();
- } catch (Exception g) {
- System.out.println("Inappropriate value!");
- input.nextLine();
- }
- if (width >= 3 && height >= 3 && marksToWin >= 3
- && marksToWin <= height && marksToWin <= width) {
- ok = false;
- } else {
- System.out.println("\n You gave bad values, try again. \n");
- }
- } while (ok);
- gameArea = new char[height][width];
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- gameArea[i][j] = ' ';
- }
- }
- }
- public static void printArea() {
- System.out.println();
- for (int i = 0; i < height; i++) {
- System.out.print(i + 1 + " ");
- for (int j = 0; j < width; j++) {
- System.out.print("[" + gameArea[i][j] + "]");
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void askPlayer() {
- int rows = 0;
- int columns = 0;
- char mark = 'X';
- boolean ok = true;
- do {
- try {
- System.out.println("In which row would ya");
- rows = input.nextInt();
- } catch (Exception k) {
- System.out.println("Inappropriate value!");
- input.nextLine();
- }
- try {
- System.out.println("In which column would ya");
- columns = input.nextInt();
- } catch (Exception j) {
- System.out.println("Inappropriate value!");
- input.nextLine();
- }
- if ((checkIfEmpty(rows, columns)) && (rows < 1 && columns < 1 && columns <= width && rows <= height)) {
- ok = false;
- }
- } while (ok);
- setMarks(rows, columns, 'X');
- }
- public static boolean checkIfEmpty(int i, int j) {
- if((i < 1 || j < 1) && (i > height || j > width)) {
- return false;
- } else if ((gameArea[i - 1][j - 1] == ' ')) {
- return true;
- }
- return false;
- }
- public static void setMarks(int i, int j, char mark) {
- gameArea[i - 1][j - 1] = mark;
- }
- public static void compRandom() {
- char mark = 'O';
- int compY = 0;
- int compX = 0;
- do {
- compY = (int) (Math.random() * height + 1);
- compX = (int) (Math.random() * width + 1);
- } while (!(checkIfEmpty(compY, compX)));
- setMarks(compY, compX, 'O');
- }
- public static void checkVictory() {
- int playercountX = 0;
- int compcountX = 0;
- int playercountY = 0;
- int compcountY = 0;
- int playercountDL = 0;
- int compcountDL = 0;
- int playercountDR = 0;
- int compcountDR = 0;
- boolean playerwin = false;
- boolean compwin = false;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- if (gameArea[i][j] == 'X') {
- playercountX++;
- compcountX = 0;
- } else if (gameArea[i][j] == 'O') {
- playercountX = 0;
- compcountX++;
- } else if (gameArea[i][j] == ' ') {
- playercountX = 0;
- compcountX = 0;
- }
- if (gameArea[j][i] == 'X') {
- playercountY++;
- compcountY = 0;
- } else if (gameArea[j][i] == 'O') {
- playercountY = 0;
- compcountY++;
- } else if (gameArea[j][i] == ' ') {
- playercountY = 0;
- compcountY = 0;
- }
- for (int k = 0; k < marksToWin; k++) {
- if ((i+k >= 0 && j+k >= 0) && (i+k < height - 1 && j+k < width - 1)) {
- if (gameArea[i+k][j+k] == 'X') {
- playercountDR++;
- compcountDR = 0;
- } else if (gameArea[i+k][j+k] == 'O') {
- playercountDR = 0;
- compcountDR++;
- } else if (gameArea[i+k][j+k] == ' ') {
- playercountDR = 0;
- compcountDR = 0;
- }
- }
- }
- for (int p = 0; p < marksToWin; p++) {
- if ((i-p >= 0) && (i-p < height - 1) && (j+p >= 0) && j+p < width -1) {
- if (gameArea[i-p][j+p] == 'X') {
- playercountDL++;
- compcountDL = 0;
- } else if (gameArea[i-p][j+p] == 'O') {
- playercountDL = 0;
- compcountDL++;
- } else if (gameArea[i-p][j+p] == ' ') {
- playercountDL = 0;
- compcountDL = 0;
- }
- }
- }
- if (playercountX == marksToWin || playercountY == marksToWin
- || playercountDL == marksToWin
- || playercountDR == marksToWin) {
- playerwin = true;
- printArea();
- System.out.println("Player won!");
- break;
- } else if (compcountX == marksToWin || compcountY == marksToWin
- || compcountDL == marksToWin
- || compcountDR == marksToWin) {
- compwin = true;
- printArea();
- System.out.println("Computer won!");
- break;
- }
- }
- }
- if (playerwin == true || compwin == true) {
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement