From Aidan Burns, 1 Week ago, written in Java.
This paste will perish in 10 Months.
This paste is a reply to tick toe from Aidan Burns
- view diff
Embed
  1. package TickTackToe;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6.  * @aidan 12/18/2019
  7.  * This program makes a tick tack toe board and has all of the necessary operators to play
  8.  */
  9. public class TickTackToe {
  10.     //declaring the variables
  11.     //board is the playing space, and when referenced, bottom right spot is 0,0  and top right is 2,2
  12.     private char[][] board;
  13.  
  14.     //p1 and p2 are the strings for the names of the players
  15.     private String p1, p2, winner;
  16.  
  17.     //A boolean value for storing who's turn it is
  18.     private boolean p1Turn;
  19.     //Boolean value to store whether or not players are still playing
  20.     private boolean stillPlaying;
  21.  
  22.  
  23.     //Class constructor
  24.     public TickTackToe(String p1Name, String p2Name){
  25.         p1 = p1Name;
  26.         p2 = p2Name;
  27.         board = new char[3][3];
  28.         setBoard(board);
  29.         p1Turn = true;
  30.         stillPlaying = true;
  31.         winner = "";
  32.     }
  33.  
  34.  
  35.     //Plays a move
  36.     public String play(int x, int y){
  37.         char c;
  38.         String currPlayer = "";
  39.         if (p1Turn){
  40.             c = 'x';
  41.             currPlayer = p1;
  42.         } else {
  43.             c = 'o';
  44.             currPlayer = p2;
  45.         }
  46.         if (!checkSpot(x,y)){
  47.             return "Spot taken, enter new spot";
  48.         } else {
  49.             setSpot(x,y,c);
  50.             switchP1Turn();
  51.         }
  52.         if (hasWon(c)) {
  53.             stillPlaying = false;
  54.             setWinner(currPlayer);
  55.             return currPlayer+" HAS WON, HAHAHAHAHAH TAKE THAT OTHER PLAYER";
  56.         }
  57.         if (stalemate()){
  58.             stillPlaying = false;
  59.             return "Stalemate";
  60.         }
  61.         return "Successfully taken";
  62.     }
  63.  
  64.  
  65.  
  66.     //inverts who is playing
  67.     public void switchP1Turn(){ p1Turn = !p1Turn; }
  68.     //sets board up to empty
  69.     public void setBoard(char[][] board) {
  70.         for(int x = 0; x < 3; x ++) {
  71.             for(int y = 0; y < 3;y++) { board[x][y] = ' '; }
  72.         }
  73.     }
  74.  
  75.     public boolean hasWon(char x){
  76.         return (rowsWin(x)||columsWin(x)||diagonalsWin(x));
  77.     }
  78.  
  79.  
  80.     public boolean stillPlaying(){ return stillPlaying;}
  81.     //checks rows to see if it wins
  82.     public boolean rowsWin(char x){
  83.         for (int r = 0; r<3;r++){
  84.             if (board[r][0]==x&&board[r][1]==x&&board[r][2]==x)
  85.                 return true;
  86.         }
  87.         return false;
  88.     }
  89.  
  90.     //checks columns to see if it wins
  91.     public boolean columsWin(char x){
  92.         for (int c = 0; c<3;c++){
  93.             if (board[0][c]==x&&board[1][c]==x&&board[2][c]==x)
  94.                 return true;
  95.         }
  96.         return false;
  97.     }
  98.  
  99.     //checks diagonals if x has won
  100.     public boolean diagonalsWin(char x){
  101.         if ((board[0][0]==x&&board[1][1]==x&&board[2][2]==x)||(board[0][2]==x&&board[1][1]==x&&board[2][0]==x))
  102.             return true;
  103.         return false;
  104.     }
  105.  
  106.     //Checks if stalemate
  107.     public boolean stalemate(){
  108.         for (int x = 0; x<3; x++){
  109.             for (int y = 0; y<3; y++){
  110.                 if (checkSpot(x,y))
  111.                     return false;
  112.             }
  113.         }
  114.         return true;
  115.     }
  116.  
  117.     //gets winner's name
  118.     public String getWinner() { return winner; }
  119.  
  120.     //sets winner
  121.     public void setWinner(String winner) { this.winner = winner; }
  122.  
  123.     //chooses a spot for a value
  124.     public void setSpot(int x, int y, char c){ board[y][x] = c; }
  125.  
  126.     //returns whether or not p1 is playing
  127.     public boolean isP1Turn(){ return p1Turn; }
  128.  
  129.     //returns value for certain spot,
  130.     public char getBoard(int x, int y) { return board[y][x]; }
  131.  
  132.  
  133.     //Class receives input of what spot, and returns true if spot is available, and false if spot is taken
  134.     public boolean checkSpot(int x, int y){
  135.         if (getBoard(x,y)=='x'||getBoard(x,y)=='o')
  136.             return false;
  137.         return true;
  138.     }
  139.  
  140. }
  141.