Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file modified out/production/TicTacToe-Java/local/jlw23339/Player.class
Binary file not shown.
Binary file not shown.
140 changes: 112 additions & 28 deletions src/local/jlw23339/Main.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
package local.jlw23339;

import java.util.Scanner;
import java.util.*;

public class Main
{

public static void main(String[] args)
{
// write your code here
char[][] board = {{' ',' ', '|', ' ',' ', '|',' ', ' '},
{'-','-', '+', '-','-', '+', '-','-'},
{' ',' ', '|', ' ',' ', '|',' ', ' '},
{'-','-', '+', '-','-', '+', '-','-'},
{' ',' ', '|', ' ',' ', '|',' ', ' '}};
char[][] board = {{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '},
{'-', '+', '-', '+', '-'},
{' ', '|', ' ', '|', ' '}};


Player p1 = new Player();
Player p2 = new Player();
System.out.println(p1.getPlayer());
System.out.println(p2.getPlayer());
printboard(board);
Scanner s = new Scanner(System.in);
System.out.println("Choose a position: ");
int position = s.nextInt();
chooseposition(position, board);
printboard(board);
Player CPU = new Player("CPU");
boolean is_running = true;
while (is_running)
{
printboard(board);
Scanner s = new Scanner(System.in);

System.out.println(p1.getPlayer() + " Choose a position: ");
int position = s.nextInt();
chooseposition(position,
board,
p1,CPU);
printboard(board);
if(p1.hasWon()){
break;
}
if((p1.getMoves().size() + CPU.getMoves().size()) == 9){
System.out.println("DRAW");
break;
}
Random r = new Random();
int randint = r.nextInt(9) + 1;

chooseposition(randint,
board,
CPU,p1);
// printboard(board);
if(CPU.hasWon()){
break;
}
System.out.println(p1.getMoves().size());
if((p1.getMoves().size() + CPU.getMoves().size()) == 9){
System.out.println("DRAW");
break;
}




}


}
public static void printboard(char[][] board){
for(char[] row : board)

public static void printboard(char[][] board)
{
for (char[] row : board)
{
for (char ch : row)
{
Expand All @@ -36,34 +71,83 @@ public static void printboard(char[][] board){
System.out.println();
}
}
public static void chooseposition(int position, char[][] board/**, User user**/){
switch(position){

public static void chooseposition(int position,
char[][] board,
Player player,Player player2)
{
Random r = new Random();
char space = ' ';
while(player.getMoves().contains(position) || player2.getMoves().contains(position)){
if (player.getName().equals("CPU"))
{
position = r.nextInt(9)+1;
}
else{
Scanner s = new Scanner(System.in);
System.out.println(player.getPlayer() + " Position already Taken; Try again.: ");
position = s.nextInt();
}

}
switch (position)
{
case 1:
board[0][0] = 'X';

board[0][0] = player.symbol;

player.setMoves(position);
break;

case 2:
board[0][3] = 'X';

board[0][2] = player.symbol;

player.setMoves(position);
break;
case 3:
board[0][6] = 'X';

board[0][4] = player.symbol;

player.setMoves(position);
break;
case 4:
board[2][0] = 'X';

board[2][0] = player.symbol;

player.setMoves(position);
break;
case 5:
board[2][3] = 'X';

board[2][2] = player.symbol;
player.setMoves(position);
break;
case 6:
board[2][6] = 'X';

board[2][4] = player.symbol;


player.setMoves(position);
break;
case 7:
board[4][0] = 'X';

board[4][0] = player.symbol;


player.setMoves(position);
break;
case 8:
board[4][3] = 'X';

board[4][2] = player.symbol;


player.setMoves(position);
break;
case 9:
board[4][6] = 'X';

board[4][4] = player.symbol;

player.setMoves(position);
break;

}
Expand Down
45 changes: 44 additions & 1 deletion src/local/jlw23339/Player.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package local.jlw23339;

import java.util.Scanner;
import java.util.*;

public class Player
{
private static int[][] wins = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};


private static int maxid;
public int id;
public String name;
public char symbol;
public ArrayList<Integer> moves = new ArrayList<>();
public Player(String name)
{
this.name = name;
maxid++;
this.id = maxid;
this.symbol = 'O';

}

public Player()
{
Expand All @@ -22,6 +34,7 @@ public Player()
this.symbol = 'O';
}
this.name = res;

}

public String getPlayer()
Expand All @@ -34,9 +47,39 @@ public String getName()
return name;
}

public ArrayList<Integer> getMoves()
{
return moves;
}

public void setMoves(int move)
{
this.moves.add(move);
}

public char getSymbol()
{

return symbol;
}
public boolean hasWon(){
for(int[] ar : wins){
int count = 0;

for( int i = 0;i<ar.length;i++){
if(moves.contains(ar[i])){
count++;
}

}
if(count == 3){
System.out.println(this.name+" wins🎉");
return true;

}

}
return false;
}
}