Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * Created by rayanelhajj98 on 25/11/2015.
- */
- public class TicTacToe {
- public static char[][] playingBoard = new char[3][3];
- public static char player1 = 'x';
- public static char player2 = 'o';
- public static int turnCounter = 0;
- public static boolean playing = true;
- public static void main(String[] args) { //changed get winner method to return void, and in the method it prints out the winner. it activates if playing is false which is done by mechanics method.
- if (playing == true){play(playingBoard);}
- }
- //print out the playing board ;
- public static void printBoard(char [][] playingBoard) {
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- System.out.print("[" + playingBoard[i][j] + "]");
- }
- System.out.println();
- }
- }
- /**
- * takes inputs from player 1 or 2 ;
- */
- public static void inputReciever(char [][] playingBoard) {
- Scanner scan = new Scanner(System.in);
- System.out.println("please enter the column number : ");
- int Cmove = scan.nextInt();
- System.out.println("please enter the row" +
- " number : ");
- int Rmove = scan.nextInt();
- // if statement to switch turn after each input;
- if (turnCounter % 2 == 0) {
- playingBoard[Rmove - 1][Cmove - 1] = player1;
- } else {
- playingBoard[Rmove - 1][Cmove - 1] = player2;
- }
- turnCounter++;
- }
- /**
- * as long as there is no winner yet it keeps the game playing
- * @param playingBoard
- */
- public static void play(char [][] playingBoard) {
- while (playing == true) {
- printBoard(playingBoard);
- if (turnCounter % 2 == 0) { // this print out whose turn it is. you can make this into a method to make it look better
- System.out.println("Player" + player1 + "'s turn:");
- } else {
- System.out.println("Player " + player2 + "'s turn:");
- }
- inputReciever(playingBoard);
- mechanics(playingBoard);
- if(playing == false){
- playAgain();
- play(playingBoard);
- }
- }
- }
- /**
- * after the game is finished or one of the players won,it starts a new game;
- */
- public static void playAgain(){
- System.out.println("if you want to play enter y if not enter n:");
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine().toUpperCase();
- if(input.equals("Y")){
- playing = true;
- for(int i= 0; i<3; i++){
- for(int j=0; j<3 ; j++){
- playingBoard[i][j] = ' ';
- }
- }
- playing = true;
- play(playingBoard);
- }
- else if(input.equals("N")){
- System.out.println("thank's for playing ! Hope to see you again. ");
- playing= false;
- }
- else if (!input.equals("Y") && !input.equals("N")){
- playing= false;
- }
- }
- /**
- * checks if we have a winner;
- * @param playingBoard
- */
- public static void mechanics(char[][] playingBoard) {
- for (int i = 0; i < 3; i++) {
- if (playingBoard[i][0] == playingBoard[i][1] && playingBoard[i][1] == playingBoard[i][2] && playingBoard[i][0] == 'x') {
- System.out.println("player 1 wins");
- playing = false;
- } else if (playingBoard[0][i] == playingBoard[1][i] && playingBoard[1][i] == playingBoard[2][i] && playingBoard[0][i] == 'x') {
- System.out.println("player 1 wins");
- playing = false;
- } else if (playingBoard[0][i] == playingBoard[1][i] && playingBoard[1][i] == playingBoard[2][i] && playingBoard[0][i] == 'o') {
- System.out.println("player 2 wins");
- playing = false;
- } else if (playingBoard[i][0] == playingBoard[i][1] && playingBoard[i][1] == playingBoard[i][2] && playingBoard[i][0] == 'o') {
- System.out.println("player 2 wins");
- playing = false;
- }
- }
- if(playingBoard[0][0] == 'x' && playingBoard[0][0]== playingBoard[1][1] && playingBoard[0][0]== playingBoard[2][2] ){
- System.out.println("player 1 wins");
- playing = false;
- }
- else if(playingBoard[0][0]=='o' && playingBoard[0][0]== playingBoard[1][1] && playingBoard[0][0]== playingBoard[2][2]){
- System.out.println("player 2 wins");
- playing = false;
- } else if ( playingBoard[0][2]== playingBoard[1][1] && playingBoard[1][1]== playingBoard[2][0] && playingBoard[0][2] == 'x') {
- System.out.println("player 1 wins");
- playing = false;
- } else if (playingBoard[0][2]== playingBoard[1][1] && playingBoard[1][1]== playingBoard[2][0] && playingBoard[0][2] == 'o') {
- System.out.println("player 2 wins");
- playing = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement