Reputation: 11
tl;dr output all is in blue for tic tac toe, only want x as blue and o as red
In class we're trying to make a tic tac toe game and currently we're trying to modify the code in such a way that only X is blue and O is red however when we import colorama it colors all of the output. I am aware that all of the text will print in blue. So essentially it should look like: I've also provided the code for the game.
import random
import colorama
from colorama import Fore, Style
print(Fore.BLUE + "Hello World")
player_1_pick = ""
player_2_pick = ""
if (player_1_pick == "" or player_2_pick == ""):
if (player_1_pick == ""):
player_1_pick = "Player 1"
if (player_2_pick == ""):
player_2_pick = "Player 2"
else:
pass
board = ["_"] * 9
def print_board():
print(board[0] + '|' + board[1] + '|' + board[2])
print(board[3] + '|' + board[4] + '|' + board[5])
print(board[6] + '|' + board[7] + '|' + board[8])
print_board()
if (random.randint(1,2) == 1):
player_1_pick = input(player_1_pick + ", choose X or O: ").upper()
if (player_1_pick == "X"):
player_2_pick = "O"
while True:
x = input('Pick a number from 0-8')
x = int(x)
board[x] = 'X'
print_board()
Then we decided to open another tab of python (we're using repl.it) to try and fix the problem in an isolated environment, in which we came up with:
import random
import colorama
from colorama import Fore, Style
def getPieceLabel(piece):
if (piece == 1):
return "|" + color.PURPLE + color.BOLD + "X" + color.END + "|"
elif (piece == 2 ):
return "|" + color.BLUE + color.BOLD + "O"
+ "|"
else:
return color.BOLD + "|_|" + color.END
board = ["_"] * 9
def print_board():
print(board[0] + '|' + board[1] + '|' + board[2])
print(board[3] + '|' + board[4] + '|' + board[5])
print(board[6] + '|' + board[7] + '|' + board[8])
print(Style.RESET_ALL)
while True:
x = input('Pick a number from 0-8')
x = int(x)
board[x] = 'X'
print_board()
We need some help figuring this out/what the problem is.
Upvotes: 0
Views: 357
Reputation: 823
Here is a code that works for printing different color for different items (tested on repl.it too!).
import random
import colorama
from colorama import Fore, Style
print(Fore.BLUE + "Tic Tac Toe")
Style.RESET_ALL
player_1_pick = ""
player_2_pick = ""
if (player_1_pick == "" or player_2_pick == ""):
if (player_1_pick == ""):
player_1_pick = "Player 1"
if (player_2_pick == ""):
player_2_pick = "Player 2"
else:
pass
board = ["_"] * 9
def print_board():
for i in range(0, 3):
for j in range(0, 3):
if (board[i*3 + j] == 'X'):
print(Fore.BLUE + board[i*3 + j], end = '')
elif (board[i*3 + j] == 'O'):
print(Fore.RED + board[i*3 + j], end = '')
else:
print(board[i*3 + j], end = '')
print(Style.RESET_ALL, end = '')
if j != 2:
print('|', end = '')
print() # new line
print_board()
if (random.randint(1,2) == 1):
player_1_pick = input(player_1_pick + ", choose X or O: ").upper()
if (player_1_pick == "X"):
player_2_pick = "O"
while True:
x = input('Pick a number from 0-8: ')
x = int(x)
board[x] = 'X'
print_board()
Why? When you use Back.RED
or Fore.RED
, it just changes the global output color (and not just that particular print()
statement). That means you must change color for every to-be-printed item if you want them in different colors. That's what I did under print_board()
.
Since you're printing a 3x3 matrix (or 2D array) along with borders between the elements or items, I had to make use of two loops to print each of them in different colors (as required).
Note: The program works, but I think there is some missing logic since the game only works for 1st player; there is no second player (or even a system player). It's out of the scope for this answer.
Upvotes: 1