Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.47 KB | None | 0 0
  1. import java.util.InputMismatchException;
  2. import java.util.Scanner;
  3.  
  4. public class TicTacToe {
  5.  
  6.     public static char[][] gameArea;
  7.  
  8.     public static Scanner input = new Scanner(System.in);
  9.  
  10.     public static int width = 0;
  11.     public static int height = 0;
  12.     public static int marksToWin = 0;
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.         iniArea();
  17.         printArea();
  18.         do {
  19.             askPlayer();
  20.             checkVictory();
  21.             compRandom();
  22.             checkVictory();
  23.             printArea();
  24.  
  25.         } while (true);
  26.  
  27.     }
  28.  
  29.     public static void iniArea() {
  30.  
  31.         boolean ok = true;
  32.         do {
  33.  
  34.             try {
  35.                 System.out.println("Give the width of the area:");
  36.                 width = input.nextInt();
  37.  
  38.             } catch (Exception e) {
  39.                 System.out.println("Inappropriate value!");
  40.  
  41.                 input.nextLine();
  42.             }
  43.             try {
  44.                 System.out.println("Give the height of the area:");
  45.                 height = input.nextInt();
  46.  
  47.             } catch (Exception f) {
  48.                 System.out.println("Inappropriate value!");
  49.  
  50.                 input.nextLine();
  51.             }
  52.             try {
  53.                 System.out
  54.                         .println("Give the amount of consecutive marks to win:");
  55.                 marksToWin = input.nextInt();
  56.  
  57.             } catch (Exception g) {
  58.                 System.out.println("Inappropriate value!");
  59.  
  60.                 input.nextLine();
  61.             }
  62.  
  63.             if (width >= 3 && height >= 3 && marksToWin >= 3
  64.                     && marksToWin <= height && marksToWin <= width) {
  65.                 ok = false;
  66.  
  67.             } else {
  68.                 System.out.println("\n You gave bad values, try again. \n");
  69.             }
  70.  
  71.         } while (ok);
  72.         gameArea = new char[height][width];
  73.  
  74.         for (int i = 0; i < height; i++) {
  75.  
  76.             for (int j = 0; j < width; j++) {
  77.  
  78.                 gameArea[i][j] = ' ';
  79.  
  80.             }
  81.         }
  82.  
  83.     }
  84.  
  85.     public static void printArea() {
  86.  
  87.         System.out.println();
  88.         for (int i = 0; i < height; i++) {
  89.             System.out.print(i + 1 + " ");
  90.             for (int j = 0; j < width; j++) {
  91.  
  92.                 System.out.print("[" + gameArea[i][j] + "]");
  93.  
  94.             }
  95.  
  96.             System.out.println();
  97.  
  98.         }
  99.  
  100.         System.out.println();
  101.  
  102.     }
  103.  
  104.     public static void askPlayer() {
  105.  
  106.         int rows = 0;
  107.         int columns = 0;
  108.         char mark = 'X';
  109.         boolean ok = true;
  110.         do {
  111.  
  112.             try {
  113.                 System.out.println("In which row would ya");
  114.                 rows = input.nextInt();
  115.             } catch (Exception k) {
  116.                 System.out.println("Inappropriate value!");
  117.                 input.nextLine();
  118.             }
  119.             try {
  120.                 System.out.println("In which column would ya");
  121.                 columns = input.nextInt();
  122.             } catch (Exception j) {
  123.                 System.out.println("Inappropriate value!");
  124.                 input.nextLine();
  125.             }
  126.             if ((checkIfEmpty(rows, columns)) && (rows < 1 && columns < 1 && columns <= width && rows <= height)) {
  127.                 ok = false;
  128.             }
  129.         } while (ok);
  130.  
  131.         setMarks(rows, columns, 'X');
  132.  
  133.     }
  134.  
  135.     public static boolean checkIfEmpty(int i, int j) {
  136.        
  137.         if((i < 1 || j < 1) && (i > height || j > width)) {
  138.             return false;
  139.         } else if ((gameArea[i - 1][j - 1] == ' ')) {
  140.             return true;
  141.         }
  142.         return false;
  143.  
  144.     }
  145.  
  146.     public static void setMarks(int i, int j, char mark) {
  147.  
  148.         gameArea[i - 1][j - 1] = mark;
  149.  
  150.     }
  151.  
  152.     public static void compRandom() {
  153.  
  154.         char mark = 'O';
  155.         int compY = 0;
  156.         int compX = 0;
  157.         do {
  158.             compY = (int) (Math.random() * height + 1);
  159.             compX = (int) (Math.random() * width + 1);
  160.         } while (!(checkIfEmpty(compY, compX)));
  161.         setMarks(compY, compX, 'O');
  162.     }
  163.  
  164.     public static void checkVictory() {
  165.  
  166.         int playercountX = 0;
  167.         int compcountX = 0;
  168.         int playercountY = 0;
  169.         int compcountY = 0;
  170.         int playercountDL = 0;
  171.         int compcountDL = 0;
  172.         int playercountDR = 0;
  173.         int compcountDR = 0;
  174.  
  175.         boolean playerwin = false;
  176.         boolean compwin = false;
  177.  
  178.         for (int i = 0; i < height; i++) {
  179.  
  180.             for (int j = 0; j < width; j++) {
  181.  
  182.                 if (gameArea[i][j] == 'X') {
  183.                     playercountX++;
  184.                     compcountX = 0;
  185.                 } else if (gameArea[i][j] == 'O') {
  186.                     playercountX = 0;
  187.                     compcountX++;
  188.                 } else if (gameArea[i][j] == ' ') {
  189.                     playercountX = 0;
  190.                     compcountX = 0;
  191.                 }
  192.                 if (gameArea[j][i] == 'X') {
  193.                     playercountY++;
  194.                     compcountY = 0;
  195.                 } else if (gameArea[j][i] == 'O') {
  196.                     playercountY = 0;
  197.                     compcountY++;
  198.                 } else if (gameArea[j][i] == ' ') {
  199.                     playercountY = 0;
  200.                     compcountY = 0;
  201.                 }
  202.                 for (int k = 0; k < marksToWin; k++) {
  203.                     if ((i+k >= 0 && j+k >= 0) && (i+k < height - 1 && j+k < width - 1)) {
  204.  
  205.                         if (gameArea[i+k][j+k] == 'X') {
  206.                             playercountDR++;
  207.                             compcountDR = 0;
  208.                         } else if (gameArea[i+k][j+k] == 'O') {
  209.                             playercountDR = 0;
  210.                             compcountDR++;
  211.                         } else if (gameArea[i+k][j+k] == ' ') {
  212.                             playercountDR = 0;
  213.                             compcountDR = 0;
  214.                         }
  215.  
  216.                     }
  217.                 }
  218.                 for (int p = 0; p < marksToWin; p++) {
  219.                     if ((i-p >= 0) && (i-p < height - 1) && (j+p >= 0) && j+p < width -1) {
  220.  
  221.                         if (gameArea[i-p][j+p] == 'X') {
  222.                             playercountDL++;
  223.                             compcountDL = 0;
  224.                         } else if (gameArea[i-p][j+p] == 'O') {
  225.                             playercountDL = 0;
  226.                             compcountDL++;
  227.                         } else if (gameArea[i-p][j+p] == ' ') {
  228.                             playercountDL = 0;
  229.                             compcountDL = 0;
  230.                         }
  231.  
  232.                     }
  233.                 }
  234.  
  235.                 if (playercountX == marksToWin || playercountY == marksToWin
  236.                         || playercountDL == marksToWin
  237.                         || playercountDR == marksToWin) {
  238.                     playerwin = true;
  239.                     printArea();
  240.                     System.out.println("Player won!");
  241.                     break;
  242.                 } else if (compcountX == marksToWin || compcountY == marksToWin
  243.                         || compcountDL == marksToWin
  244.                         || compcountDR == marksToWin) {
  245.                     compwin = true;
  246.                     printArea();
  247.                     System.out.println("Computer won!");
  248.                     break;
  249.                 }
  250.  
  251.             }
  252.  
  253.         }
  254.  
  255.         if (playerwin == true || compwin == true) {
  256.            
  257.             System.exit(0);
  258.         }
  259.  
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement