Reputation: 23
Alright so I have to make a random tictactoe checker that shows when x wins, when o wins, and when there is a tie. So the issues I'm having is it wont show the ties and it will sometimes say that either x or o won when they didn't. I don't know what to change around in my code because before I did my diagonal check it would print out the ties. Here is the whole code but I'm pretty sure the problem is coming from the checking board part by making xWin and oWin come out true I can't find where its doing that tho.
package test;
import java.util.Scanner;
import java.util.Random;
public class TicTacToe {
public static void main(String[] args) {
System.out.println("Welcome to random Tic Tac Toe Checker. Let's see our randomly generated board.");
int dimension = 3;
char[][] board = new char[dimension][dimension];
Random r = new Random();
for (int i = 0; i < 3; i++) // filling board
{
for (int j = 0; j < 3; j++) {
int choice = r.nextInt(2);
if (choice == 0) {
board[i][j] = 'X';
} else if (choice == 1) {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < 3; i++) // filling board
{
for (int j = 0; j < 3; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
boolean xWin = false;// checking board, order horizontal,vertical,left
// and right diagonal
boolean oWin = false;
for (int i = 0; i < 3; i++) {
boolean lineWin = true;
for (int j = 0; j < 3; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin == true) {
if (board[i][0] == 'X') {
xWin = false;
}
if (board[i][0] == 'O') {
oWin = false;
}
}
}
for (int j = 0; j < 3; j++) {
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[i][j] != board[0][j]) {
lineWin = true;
}
}
if (lineWin == true) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
}
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[0][0] != board[i][i]) {
lineWin = false;
}
if (lineWin == true) {
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
}
lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[0][0] != board[i][2 - i]) {
lineWin = false;
}
if (lineWin == true) {
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
}
if (xWin == false && oWin == false)// printing winners
{
System.out.println("CAT!It's a tie no one wins");
}
if (xWin == true) {
System.out.println("X wins!");
}
if (oWin == true) {
System.out.println("O wins!");
}
}
}
Upvotes: 2
Views: 190
Reputation: 5093
I fixed the boolean errors as well as the diagonal logic:
import java.util.Random;
public class TicTacToe {
private static final int DIMENSION = 3;
public static void main(String[] args) {
System.out.println("Welcome to random Tic Tac Toe Checker. Let's see our randomly generated board.");
char[][] board = new char[DIMENSION][DIMENSION];
final Random r = new Random();
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
int choice = r.nextInt(2);
if (choice == 0) {
board[i][j] = 'X';
} else if (choice == 1) {
board[i][j] = 'O';
}
}
}
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
boolean xWin = false;
boolean oWin = false;
for (int i = 0; i < DIMENSION; i++) {
boolean lineWin = true;
for (int j = 0; j < DIMENSION; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin) {
if (board[i][0] == 'X') {
xWin = true;
}
if (board[i][0] == 'O') {
oWin = true;
}
}
}
for (int j = 0; j < DIMENSION; j++) {
boolean lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[i][j] != board[0][j]) {
lineWin = false;
}
}
if (lineWin) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
}
boolean lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[0][0] != board[i][i]) {
lineWin = false;
}
}
if (lineWin) {//this check should not be in the loop
if (board[0][0] == 'X') {
xWin = true;
}
if (board[0][0] == 'O') {
oWin = true;
}
}
lineWin = true;
for (int i = 0; i < DIMENSION; i++) {
if (board[DIMENSION - 1][0] != board[i][DIMENSION - 1 - i]) {
lineWin = false;
}
}
if (lineWin) {//this check should not be in the loop
if (board[DIMENSION - 1][0] == 'X') {
xWin = true;
}
if (board[DIMENSION - 1][0] == 'O') {
oWin = true;
}
}
if (xWin == true && oWin == true) {//printing winners
System.out.println("Both players won!");
}
if (xWin == false && oWin == false) {
System.out.println("CAT!It's a tie no one wins");
}
if (xWin == true) {
System.out.println("X wins!");
}
if (oWin == true) {
System.out.println("O wins!");
}
}
}
Note: you can increase DIMENSION for a laugh.
Upvotes: 2
Reputation: 17605
In the first block
if(lineWin == true)
{
if(board[i][0] == 'X')
{
xWin = false;
}
if(board[i][0] == 'O')
{
oWin = false;
}
}
should be changed as follows.
if(lineWin == true)
{
if(board[i][0] == 'X')
{
xWin = true;
}
if(board[i][0] == 'O')
{
oWin = true;
}
}
Furthermore, in the second block, the part
if(board[i][j] != board[0][j])
{
lineWin = true;
}
should be changed as follows.
if(board[i][j] != board[0][j])
{
lineWin = false;
}
Upvotes: 0
Reputation: 1
I think the main problem in this file comes around the middle, specifically lines 34 - 66, where you do the initial checks for horizontal and vertical rows of the same character.
for (int i = 0; i < 3; i++) {
boolean lineWin = true;
for (int j = 0; j < 3; j++) {
if (board[i][j] != board[i][0]) {
lineWin = false;
}
}
if (lineWin == true) {
if (board[i][0] == 'X') {
xWin = false; //THESE LINES SHOULD BE CHANGING THE WIN CONDITION TO TRUE
}
if (board[i][0] == 'O') {
oWin = false; //LISTED ABOVE
}
}
}
for (int j = 0; j < 3; j++) {
boolean lineWin = true;
for (int i = 0; i < 3; i++) {
if (board[i][j] != board[0][j]) {
lineWin = true; //THIS LINE SHOULD BE FALSE
}
}
if (lineWin == true) {
if (board[0][j] == 'X') {
xWin = true;
}
if (board[0][j] == 'O') {
oWin = true;
}
}
I listed some changes that will fix the code and make it run the way you want it to. It looks like there were some errors in declaring a boolean to be true
or false
at the wrong time, so this should be able to fix the problem.
Upvotes: 0