Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Homework3;
- import java.util.Random;
- import java.util.Scanner;
- public class DotsAndDaggers {
- private static char map[][];
- private static int size = 3;
- private static char empty = '*';
- private static char dot = 'O';
- private static char dagger = 'X';
- private static byte charInLineToWin = 3;
- private static byte score = 0;
- private static Scanner scanner = new Scanner(System.in);
- private static Random random = new Random();
- public static void main(String[] args) {
- initMap();
- printMap();
- while (true) {
- userTurn();
- if (isEndGame(dagger) == true) {
- break;
- }
- computerTurn();
- if (isEndGame(dot)) {
- break;
- }
- }
- System.out.printf("Game over");
- }
- public static void initMap() {
- map = new char[size][size];
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- map[i][j] = empty;
- }
- }
- }
- public static void printMap(){
- System.out.print("\t");
- for (int i = 1; i < size + 1; i++) {
- System.out.print(i + "\t");
- }
- System.out.println();
- System.out.println();
- for (int i = 0; i < size; i++) {
- System.out.print((i + 1) + "\t");
- for (int j = 0; j < size; j++) {
- System.out.print(map[i][j] + "\t");
- }
- System.out.println();
- }
- System.out.println();
- }
- public static void userTurn(){
- System.out.println("User turn");
- int x, y;
- do {
- System.out.print("Enter coordinates of cell: ");
- x = scanner.nextInt() - 1;
- y = scanner.nextInt() - 1;
- } while (!isCellValid(y, x));
- map[x][y] = dagger;
- }
- public static void computerTurn(){
- int good[][] = new int[size][size];
- int x, y;
- do {
- x = random.nextInt(size);
- y = random.nextInt(size);
- } while (map[x][y] != empty);
- System.out.println("computer made it's turn in cell " + (x + 1) + " " + (y + 1));
- System.out.println();
- map[x][y] = dot;
- }
- private static boolean isCellValid(int x, int y){
- boolean result = true;
- if ((map[y][x] != empty) || x >= size || x < 0 || y >= size || y < 0) {
- result = false;
- }
- return result;
- }
- public static boolean isEndGame(char symbol){
- boolean result = false;
- printMap();
- if (isAnyWon(symbol)) {
- System.out.println(symbol + " won");
- result = true;
- }
- if (isMapFull() == true && isAnyWon(symbol) != true){
- System.out.println("nobody won");
- result = true;
- }
- return result;
- }
- public static boolean isAnyWon(char symbol){
- score = 0;
- for (int i = 0; i < map.length; i++) {
- if (map[i][i] == symbol) {
- score++;
- if (score == charInLineToWin) {
- return true;
- }
- }
- }
- score = 0;
- for (int i = 0; i < map.length; i++) {
- if (map[i][map.length - i - 1] == symbol) {
- score++;
- if (score == charInLineToWin) {
- return true;
- }
- }
- }
- score = 0;
- if (winInCaseColumn(3, symbol) == true) {
- return true;
- }
- score = 0;
- if (winInCaseColumn(2, symbol) == true) {
- return true;
- }
- score = 0;
- if (winInCaseColumn(1, symbol) == true) {
- return true;
- }
- score = 0;
- if (winInCaseLine(3, symbol) == true) {
- return true;
- }
- score = 0;
- if (winInCaseLine(2, symbol) == true) {
- return true;
- }
- score = 0;
- if (winInCaseLine(1, symbol) == true) {
- return true;
- }
- return false;
- }
- public static boolean winInCaseColumn(int x, char symbol){
- boolean result = false;
- for (int i = 0; i < size; i++) {
- if (map[i][map.length - x] == symbol) {
- score++;
- }
- if (score == charInLineToWin) {
- result = true;
- }
- }
- return result;
- }
- public static boolean winInCaseLine(int x, char symbol){
- boolean result = false;
- for (int i = 0; i < size; i++) {
- if (map[map.length - x][i] == symbol) {
- score++;
- }
- if (score == charInLineToWin) {
- result = true;
- }
- }
- return result;
- }
- public static boolean isMapFull(){
- boolean result = true;
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (map[i][j] == empty) {
- result = false;
- }
- }
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment