-
-
Save arushi011/ca59ca825fa885508b88 to your computer and use it in GitHub Desktop.
Tic - Tac - Toe game in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import java.io.IOException; | |
import java.util.Scanner; | |
class playutility | |
{ //by default interface variables are static and final | |
int winner = 0, count = 0; | |
static int yes=1; | |
static Scanner scan = new Scanner(System.in); | |
String data[] = new String[9]; | |
int index, flag, i, k, j; | |
String sign; | |
static int player; | |
} | |
class B extends playutility | |
{ | |
public void playingLogic() | |
{ initialize(); | |
while (count < 9) | |
{ | |
display(); | |
player(); | |
System.out.println("move for player "+player); | |
getIndex(); //checkAvailibility + takes input | |
//storeIndex(); | |
if(yes==0) | |
continue; | |
System.out.println("yep"); | |
checkIndex(); // checkHorizontal + checkVertical + checkDiagonal and decides value if winner | |
if(winner==1) | |
break; | |
count++; | |
} | |
displayWinner(); | |
} | |
private void initialize() | |
{ | |
for (i=0 ;i < 9; i++) | |
data[i] = " "; | |
} | |
private void display() | |
{ //try { | |
//Runtime.getRuntime().exec("cls"); | |
//} catch (Exception e){} | |
//System("CLS"); | |
System.out.println("\n\n"); | |
System.out.println("\t\t\t"+ data[0]+" | "+ data[1]+" | "+ data[2]+"\n"); | |
System.out.println("\t\t\t---------\n"); | |
System.out.println("\t\t\t"+ data[3]+" | "+ data[4]+" | "+ data[5]+"\n"); | |
System.out.println("\t\t\t---------\n"); | |
System.out.println("\t\t\t" +data[6]+" | "+ data[7]+" | "+ data[8]+"\n"); | |
} | |
private void player() //selects player //abstraction | |
{ | |
if (count % 2 == 0) | |
{ | |
sign = "X"; | |
player = 1; | |
} | |
else { | |
sign = "Y"; | |
player = 2; | |
} | |
} | |
private void getIndex() | |
//>> { if(checkAvailable()==1) | |
{ | |
index = scan.nextInt(); | |
// scan.next(); | |
checkAvailable(); | |
if(yes==0) | |
{ // scan.next(); | |
// scan.close(); | |
return;} | |
data[index - 1] = sign; | |
//scan.nextInt(); | |
//scan.close(); | |
//} | |
} | |
private void checkAvailable() | |
{ | |
if (index < 1 || index > 9) { | |
System.out.println("Allowed index is 1 to 9!!\n"); | |
yes=0; | |
} | |
//continue; | |
else if (data[index - 1].equals("X") || data[index - 1].equals("Y")) | |
{ | |
System.out.println("Position Already occupied!!\n"); | |
yes=0; | |
} | |
// continue; | |
else yes = 1; | |
} | |
private void checkIndex() { | |
checkHorizontal(); | |
checkVertical(); | |
checkDiagonal(); | |
} | |
private void checkHorizontal(){ | |
for (i = 0; i < 9; i++) | |
{if (i % 3 == 0) | |
flag = 0; | |
if (data[i].equals(sign)) | |
flag++; | |
if (flag == 3) { | |
winner = 1; | |
//goto win; | |
} | |
} | |
} | |
private void checkVertical(){ | |
for (i = 0; i < 3; i++) { | |
for (k = i; k <= i + 6; k = k + 3) { | |
flag = 0; | |
if (data[k] == sign) | |
flag++; | |
} | |
if (flag == 3) { | |
winner = 1; | |
//goto win; | |
} | |
} | |
} | |
private void checkDiagonal(){ | |
if ((data[0].equals(sign) && data[4].equals(sign) && data[8].equals(sign)) || | |
(data[2].equals(sign) && data[4].equals(sign) && data[6].equals(sign))) { | |
winner = 1;} | |
} | |
private void displayWinner() | |
{ | |
//System.out.println("\f"); | |
display(); | |
if (winner==1) { | |
System.out.println("Player "+player+" is the winner. Congrats!!\n"); | |
} | |
else | |
{System.out.println("Match draw.. Best of luck for both\n"); | |
} | |
} | |
} | |
public class ticTacToe{ | |
public static void main(String[] args){ | |
B obj = new B(); | |
obj.playingLogic(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment