import java.util.Scanner;
import java.util.Random;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class TicTacToe {
static ArrayList<Integer> playerPositions = new ArrayList<Integer>();
static ArrayList<Integer> cpuPositions = new ArrayList<Integer>();
public static void main(String[] args) {
char[][] gameBoard = {
{'|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'|', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '|'},
{'|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'|', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '|'},
{'|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'|', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '|'},
{'|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'|', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '|'},
{'|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'|', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '|'},
{'|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|', ' ', '|'},
{'|', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '+', '-', '|'}
};
printGameBoard(gameBoard);
while(true) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter your placememt (x,y):");
int playerPos = scan.nextInt();
while(playerPositions.contains(playerPos) || cpuPositions.contains(playerPos)) {
placePiece(gameBoard, playerPos, "player");
playerPos = scan.nextInt();
}
placePiece(gameBoard, playerPos, "player");
String result = checkWinner();
if(result.length() > 0){
printGameBoard(gameBoard);
System.out.println(result);
break;
}
Random rand = new Random();
int cpuPos = rand.nextInt(9) + 1;
while(playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos)) {
cpuPos = rand.nextInt(9) + 1;
}
placePiece(gameBoard, cpuPos, "cpu");
printGameBoard(gameBoard);
result = checkWinner();
if(result.length() > 0){
System.out.println(result);
break;
}
}
}
public static void printGameBoard(char[][] gameBoard) {
for(char[] row : gameBoard) {
for(char c : row) {
System.out.print(c);
}
System.out.println();
}
}
public static void placePiece(char[][] gameBoard, int pos, String user) {
char symbol=" ";
if(user.equals("player")) {
symbol="X";
playerPositions.add(pos);
} else if(user.equals("cpu")) {
symbol="O";
cpuPositions.add(pos);
}
switch(pos) {
case 11:
gameBoard[10][1] = symbol;
break;
case 12:
gameBoard[10][3] = symbol;
break;
case 13:
gameBoard[10][5] = symbol;
break;
case 14:
gameBoard[10][7] = symbol;
break;
case 15:
gameBoard[10][9] = symbol;
break;
case 16:
gameBoard[10][11] = symbol;
break;
case 17:
gameBoard[10][13] = symbol;
break;
case 21:
gameBoard[8][1] = symbol;
break;
case 22:
gameBoard[8][3] = symbol;
break;
case 23:
gameBoard[8][5] = symbol;
break;
case 24:
gameBoard[8][7] = symbol;
break;
case 25:
gameBoard[8][9] = symbol;
break;
case 26:
gameBoard[8][11] = symbol;
break;
case 27:
gameBoard[8][13] = symbol;
break;
case 31:
gameBoard[6][1] = symbol;
break;
case 32:
gameBoard[6][3] = symbol;
break;
case 33:
gameBoard[6][5] = symbol;
break;
case 34:
gameBoard[6][7] = symbol;
break;
case 35:
gameBoard[6][9] = symbol;
break;
case 36:
gameBoard[6][11] = symbol;
break;
case 37:
gameBoard[6][13] = symbol;
break;
case 41:
gameBoard[4][1] = symbol;
break;
case 42:
gameBoard[4][3] = symbol;
break;
case 43:
gameBoard[4][5] = symbol;
break;
case 44:
gameBoard[4][7] = symbol;
break;
case 45:
gameBoard[4][9] = symbol;
break;
case 46:
gameBoard[4][11] = symbol;
break;
case 47:
gameBoard[4][13] = symbol;
break;
case 51:
gameBoard[2][1] = symbol;
break;
case 52:
gameBoard[2][3] = symbol;
break;
case 53:
gameBoard[2][5] = symbol;
break;
case 54:
gameBoard[2][7] = symbol;
break;
case 55:
gameBoard[2][9] = symbol;
break;
case 56:
gameBoard[2][11] = symbol;
break;
case 57:
gameBoard[2][13] = symbol;
break;
case 61:
gameBoard[0][1] = symbol;
break;
case 62:
gameBoard[0][3] = symbol;
break;
case 63:
gameBoard[0][5] = symbol;
break;
case 64:
gameBoard[0][7] = symbol;
break;
case 65:
gameBoard[0][9] = symbol;
break;
case 66:
gameBoard[0][11] = symbol;
break;
case 67:
gameBoard[0][13] = symbol;
break;
default:
break;
}
}
public static String checkWinner() {
List OneRow1 = Arrays.asList(11,12,13,14);
List OneRow2 = Arrays.asList(12,13,14,15);
List OneRow3 = Arrays.asList(13,14,15,16);
List OneRow4 = Arrays.asList(14,15,16,17);
List TwoRow1 = Arrays.asList(21,22,23,24);
List TwoRow2 = Arrays.asList(22,23,24,25);
List TwoRow3 = Arrays.asList(23,24,25,26);
List TwoRow4 = Arrays.asList(24,25,26,27);
List ThrRow1 = Arrays.asList(31,32,33,34);
List ThrRow2 = Arrays.asList(32,33,34,35);
List ThrRow3 = Arrays.asList(33,34,35,36);
List ThrRow4 = Arrays.asList(34,35,36,37);
List FouRow1 = Arrays.asList(41,42,43,44);
List FouRow2 = Arrays.asList(42,43,44,45);
List FouRow3 = Arrays.asList(43,44,45,46);
List FouRow4 = Arrays.asList(44,45,46,47);
List FivRow1 = Arrays.asList(51,52,53,54);
List FivRow2 = Arrays.asList(52,53,54,55);
List FivRow3 = Arrays.asList(53,54,55,56);
List FivRow4 = Arrays.asList(54,55,56,57);
List SixRow1 = Arrays.asList(61,62,63,64);
List SixRow2 = Arrays.asList(62,63,64,65);
List SixRow3 = Arrays.asList(63,64,65,66);
List SixRow4 = Arrays.asList(64,65,66,67);
List<List> winning = new ArrayList<List>();
winning.add(OneRow1);
winning.add(OneRow2);
winning.add(OneRow3);
winning.add(OneRow4);
winning.add(TwoRow1);
winning.add(TwoRow2);
winning.add(TwoRow3);
winning.add(TwoRow4);
winning.add(ThrRow1);
winning.add(ThrRow2);
winning.add(ThrRow3);
winning.add(ThrRow4);
winning.add(FouRow1);
winning.add(FouRow2);
winning.add(FouRow3);
winning.add(FouRow4);
winning.add(FivRow1);
winning.add(FivRow2);
winning.add(FivRow3);
winning.add(FivRow4);
winning.add(SixRow1);
winning.add(SixRow2);
winning.add(SixRow3);
winning.add(SixRow4);
for(List l : winning) {
if(playerPositions.containsAll(l)) {
return "Congratulations you won!";
} else if(cpuPositions.containsAll(l)) {
return "You suck git gud XD";
} else if(playerPositions.size() + cpuPositions.size() == 42){
return "You tied an rng, that's sad :(";
}
}
return"";
}
}
So I have this switch and some cases for each point on the board and
I can’t for the life of me figure out how to move symbols to the point above them if that point on the board has already been placed.
I tried using a “while” like,
while(playerPositions.contains(cpuPos) || cpuPositions.contains(cpuPos))
coupled with an if statement and the points, but it proved unsuccessful. When I found some way to make the point move using this, the board wouldn’t print until both points were taken. Any advice for a newbie like me?
-Note: thank you for the comments, this is my very first posted question so the feedback on formatting my question and such is GREATLY appriciated.
The first thing you should do is research the distinction between “Java” and “JavaScript”.
Your first code needs a closing
}
or it won’t compile. The second needs a statement or semicolon after it or it won’t compile. I have no idea what any of it is supposed to do, so can’t help more than that. I’m guessing you want the general algorithm? Maybe search for code (e.g GitHub) to see examples of what to do.You really should be posting more (probably all) of your code, demonstrating how you’re attempting to move the counter to the position above. Off hand, you should really be testing whether a cell is open before trying to cram another counter inside, but seeing your actual code would help. Also, using a switch statement there has a certain code smell to it, immediately, but again, hard to judge without seeing code. Edit your question to include more code or you’re unlikely to get any sort of useful answer.