Software Development and Programming Careers (Official Discussion Thread)

Freedman

Choppers For Karate Nggas
Joined
Jun 27, 2012
Messages
18,027
Reputation
5,995
Daps
88,682
Reppin
Louisiana
Reached the first milestone project on my Udemy Python course....


...a tictactoe game.
:yeshrug:
Thinking of setting up the board as a series of three lists, like this...(I don't know how to set up a matrix yet)
[1,2,3]
[4,5,6]
[7,8,9]

Then creating a function / loop that asks the Player where they'd ike to place their X (player 1), or O (player 2).

Then add logic that checks if there are three X/O's aligned and have that player win the game.

Will report back and post the code here later...
Never coded in python but from what I can tell list seem similar to arrays so I'd go with the multi dimensional array suggestion above and set it up with all values of 0 and change the values for Xs and Os.

// initial board

list =[ [ 0, 0, 0],
[0, 0, 0],
[0,0,0] ]

int x = 1;
int o = 2;

// player 1 puts an x
list[0][0] = x;
// player 2 puts an o
list[2][2] = o;
// player 1
list[0][1] = x;
// player 2
list[2][1] = o;

board should then look like this

[ [1, 1, 0] ,
[0, 0 , 0] ,
[0, 2, 2] ]

Not sure if this is like something for the console or Like with actual squares on the screen so you need to work on the function to actually place the X/O and change the value and the function to check if anybody has won. Probably could use nested for loops for the last part to check the whole board
 
Last edited:

levitate

I love you, you know.
Joined
Sep 3, 2015
Messages
38,922
Reputation
5,722
Daps
147,619
Reppin
The Multiverse
Never coded in python but from what I can tell list seem similar to arrays so I'd go with the multi dimensional array suggestion above and set it up with all values of 0 and change the values for Xs and Os.

// initial board

list =[ [ 0, 0, 0],
[0, 0, 0],
[0,0,0] ]

int x = 1;
int o = 2;

// player 1 puts an x
list[0][0] = x;
// player 2 puts an o
list[2][2] = o;
// player 1
list[0][1] = x;
// player 2
list[2][1] = o;

board should then look like this

[ [1, 1, 0] ,
[0, 0 , 0] ,
[0, 2, 2] ]

Not sure if this is like something for the console or Like with actual squares on the screen so you need to work on the function to actually place the X/O and change the value and the function to check if anybody has won. Probably could use nested for loops for the last part to check the whole board

:ehh:

I wanted to have the board displayed with 1-9 values so that the player can more directly tell me where they'd like to place their X/O.

[1,2,3]
[4,5,6]
[7,8,9]

selection = input("Player 1, where would like to place your X? Select location 1-9")

If they select a location that's already taken I can come back at them...print("Sorry Breh, that location is already full, select again...")

Anyway, I'm running into an issue getting the items in the rows to change values (example, row1, from 3 to X):sadcam:

for item in row1:
if item == selection:
item = "X'

Basically I'm saying OK, Player 1 chose to place thier X in location 3 (row1). Now let's go ahead and change that value to "X". Now the board should look like...
[1,2,X]
[4,5,6]
[7,8,9]

shyt ain't working...
:shaq2:
 

Obreh Winfrey

Truly Brehthtaking
Supporter
Joined
Nov 18, 2016
Messages
20,851
Reputation
25,252
Daps
131,928
:ehh:

I wanted to have the board displayed with 1-9 values so that the player can more directly tell me where they'd like to place their X/O.

[1,2,3]
[4,5,6]
[7,8,9]

selection = input("Player 1, where would like to place your X? Select location 1-9")

If they select a location that's already taken I can come back at them...print("Sorry Breh, that location is already full, select again...")

Anyway, I'm running into an issue getting the items in the rows to change values (example, row1, from 3 to X):sadcam:

for item in row1:
if item == selection:
item = "X'

Basically I'm saying OK, Player 1 chose to place thier X in location 3 (row1). Now let's go ahead and change that value to "X". Now the board should look like...
[1,2,X]
[4,5,6]
[7,8,9]

shyt ain't working...
:shaq2:
What about it isn't working?
 

Matt504

YSL as a gang must end
Joined
Sep 7, 2013
Messages
45,200
Reputation
14,767
Daps
273,895
:ehh:

I wanted to have the board displayed with 1-9 values so that the player can more directly tell me where they'd like to place their X/O.

[1,2,3]
[4,5,6]
[7,8,9]

selection = input("Player 1, where would like to place your X? Select location 1-9")

If they select a location that's already taken I can come back at them...print("Sorry Breh, that location is already full, select again...")

Anyway, I'm running into an issue getting the items in the rows to change values (example, row1, from 3 to X):sadcam:

for item in row1:
if item == selection:
item = "X'

Basically I'm saying OK, Player 1 chose to place thier X in location 3 (row1). Now let's go ahead and change that value to "X". Now the board should look like...
[1,2,X]
[4,5,6]
[7,8,9]

shyt ain't working...
:shaq2:

Which language are you writing this in, it's possible that the reason you can't "update" the value in the array is that the array is immutable.
 

Double J

Banned
Joined
May 11, 2012
Messages
1,929
Reputation
-655
Daps
5,264
Which language are you writing this in, it's possible that the reason you can't "update" the value in the array is that the array is immutable.
The only thing that is immutable about an array is it's length.
 

Double J

Banned
Joined
May 11, 2012
Messages
1,929
Reputation
-655
Daps
5,264
in javascript, arrays are mutable.

Code:
let array = [1,2,3,4,5]

array.push(6);

//array = [1,2,3,4,5,6]
:ehh:

Well I'm coming from Java so I didn't know that. Only dabbled in JavaScript.

But what did you mean when you said his problem could be stemming from the array he is using being immutable?
 

Matt504

YSL as a gang must end
Joined
Sep 7, 2013
Messages
45,200
Reputation
14,767
Daps
273,895
:ehh:

Well I'm coming from Java so I didn't know that. Only dabbled in JavaScript.

But what did you mean when you said his problem could be stemming from the array he is using being immutable?

if you're not able to change the value of 0 to x || o, it seems that the value is unable to be changed.
 

Double J

Banned
Joined
May 11, 2012
Messages
1,929
Reputation
-655
Daps
5,264
if you're not able to change the value of 0 to x || o, it seems that the value is unable to be changed.
Well that would have to mean whatever data type he's storing in the array is declared as final or an immutable data type right?
 

Matt504

YSL as a gang must end
Joined
Sep 7, 2013
Messages
45,200
Reputation
14,767
Daps
273,895
Well that would have to mean whatever data type he's storing in the array is declared as final or an immutable data type right?

that would be my suspicion. that's why I was trying to get an idea of what language he's writing it in.
 

Obreh Winfrey

Truly Brehthtaking
Supporter
Joined
Nov 18, 2016
Messages
20,851
Reputation
25,252
Daps
131,928
that would be my suspicion. that's why I was trying to get an idea of what language he's writing it in.
He's using Python. Typing isn't an issue for him, it's probably just his logic to place tokens. I've been halfway turning over an idea to place in a location without using a loop. If I feel like it I might write it out and post it.
 

Rozay Oro

2 Peter 3:9 if you don’t know God
Supporter
Joined
Sep 9, 2013
Messages
41,252
Reputation
5,272
Daps
75,044
What will get me to the bread quicker freelance wise. Web dev or programming?
 
Top