New answers tagged

0

Displaying the grid Personally, I would move displaying the grid into a separate function, such as display_grid: def display_grid() -> None: for row in game: print(row) This removes the need to keep having to write the loop over and over. Picking the player Another personal preference is that I would have the user input both the row and ...


2

Player identification #define Player1 1 #define Player2 2 int whoseTurn; Do you ever plan to support more than two players? If not, consider representing this as enum or a bool. The same is true of your moves and board. Those should not be strings; they should be enums. Making them strings is not strongly-typed, and gives you fewer guarantees about the ...


1

I think the nicest thing to do is to split your task into three bits, essentially you want to see if a row, column or diagonal has all the entries the same. A row, column or diagonal can all be thought of just as a (1D) array. In fact, these things are all the 'lines' (pictorially) that you can find in a 2D array (which span the whole array). Now given a ...


2

The code generally looks good, but is a little rough to read (in part due to length but also with method names like turnIndicatorToggle(self) and toggleTurnIndicator(self)). But when I try to run your program I run into a few issues; your main window is fairly huge (bigger than my screen!) and can't be resized, and your indicators for tics and tacs (x and o) ...


2

In addition to what @K.H. already mentioned: Comments to the code Things will get simplified if you represent the Board as a set of numbers Board = set([1,2,3,4,5,6,7,8,9]) Then Board.remove(z) would look better than doing manipulations with indices and assigning an empty list (Board[z-1] = []) The condition below is not really used: while len(PlayerO) ...


5

I guess from efficiency point of view this is fine, even that last line. Efficiency points: You create player variable as list and then keep converting to set over and over again, why not have it as set from beginning? From readability and maintenance point of view, you are having lots of duplicities. Player moves are exactly same except for message and ...


2

I really don't like this svg code in javascript string. Why don't you put in html instead? It's presentation detail of your X and O has no meaning in your code. Edit: or even separate .svg file Extract your data from HTML. Your "data" are in html elements and innerHTML. That's just too tight. Your code is mix of different responsibilities (game itself, UI). ...


2

if (!item.innerHTML) { // Because "" is falsy. If you need to add a comment then maybe the code isn't clear enough. I suggest either comparing it implicitly to an empty string, or using a data attribute to mark it as filled. In checkWin and the onClick listener, instead of relying on the innerHTML value, you should either use a virtual grid state, or save ...


6

PEP 8 Overall, your code is very clean. However, there are a few minor PEP-8 deviations: .getname() should be .get_name(), as it is two words. In several places, where a statement continues over multiple lines, the indentation is not PEP-8 compliant, such as: self.board = {1: ' ', 2: ' ', 3: ' ', 4: ' ', 5: ' ', 6: ' ', 7: ' ', 8: ...


Top 50 recent answers are included