New answers tagged

1

This is not a complete answer, just a small point: while valid_choice is False: is more Pythonic written as while not valid_user_input:


3

Readability Break the code up into multiple functions, that each do their own small task. The main loop should read like: for player in itertools.cycle(players): spot = pick_spot(board) place_mark(board, player, spot) print(board) if (check_win_condition(board)): print(f"{player} wins!") break elif (...


3

In check_win: checks = {True if (len(set(lst)) == 1 and lst[0] != "-") else False for lst in checks} if True in checks: return True return False can be rewritten more explicitly as: return any(len(set(lst)) == 1 and lst[0] != "-" for lst in checks)


3

Welcome to CodeReview! You have missed an OO-opportunity. You have a class Player but you are still "switching on internal data". You do this: def get_player_input(choices, text=''): ... which is not a method on Player. And later, you do this: if player.is_human: row = int(get_player_input(options, "Enter row number(1-3): ")) - 1 ...


5

Welcome to Code Review! Having a class for player seems over complicated. A simple namedtuple would be enough. The main() function is doing most of the heavy lifting. You can have a Game class, which takes the players list (or individual objects) as init parameters, and then implements the game logic. You can reuse the get_player_input when asking for ...


1

This looks good and well thought through. The continue, break and return statements are always a bit tricky, it might be useful to add a comment to them to explain what they continue/break/return from. Theoretically we could be in an endless loop if the computer player never finds a non-full tile.... , but the AI algorithm you planned will fix that :-)


2

Instead of using indices from 0 to 9, change the way you store your board to be a 3x3 list. board = list([[0] * 3] * 3) """ 1 | 2 | 3 4 | 5 | 6 7 | 8 | 9 """ This will allow you to come back to it and add a GUI. It also becomes easier for anyone reading the code to understand what's happening visually. Next is your printout of ...


2

First thing, I assume that you don't like to use Object Oriented concepts and we will continue by the function approach. One thing that I should say to you about the comments is always to use them but in a meaningful way. Adding # Variables comment above of the variables will not add any additional value to your code. It only wastes space. The second thing ...


3

Program looks well structured, so this is gonna be nitpicky function isDiagonalWinner(id) { posGradient = true; Since posGradient hasn't been declared anywhere, this will be treated as a global on window['posGradient']. let would suit you nicely here. var matches = document.querySelectorAll("#board .row .elem .clickable"); The value of ...


7

Returning by reference ttt_init makes an instance of your TTT_BoardGame but that instance has to be copied when returning it. This cannot be done efficiently. Your struct is so trivially small that no performance impact can be noticeable, but since you're expressly doing this for learning, it's worth pursuing the idiomatic method instead: Either accept a ...


0

When I run the game, it allows me to select a side. I selected 'x'. Then the display changed to add the 'o' side, then the display commenced to flash with no (discernible) way for me/computer to make a move. suggest the system( "clear" ); only be called after a side inputs a move.


3

As both players are handled the same: // TODO: define marker = char array {' ', 'X', 'O'} void switchPlayers(){ printf(" Player %d choose: ", player); scanf("%d", &choice); // I inverted logic of the if! if (!markBoard(marker[player])) { player = 3 - player; } // ... the else can be dropped as ...


2

In addition to what's already been said, here's some misc remarks about using obsolete style: void switchPlayers(); Functions with an empty parenthesis is obsolete style in C since over 20 years back. You should be writing void switchPlayers(void); instead, because in C the () means "accept any parameter", which is type unsafe and outdated style. (...


Top 50 recent answers are included