Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary and List
#1
new_row1 = [' ', '|', ' ', '|', ' ']
new_row2 = [' ', '|', ' ', '|', ' ']
new_row3 = [' ', '|', ' ', '|', ' ']

def displayBoard(row1, row2, row3, playerMarker, comMarker, whoseTurn, move):
    rowChoice = {7: row1[0], 8: row1[2], 9: row1[4],
                 4: row2[0], 5: row2[2], 6: row2[4],
                 1: row3[0], 2: row3[2], 3: row3[4], 10: row1[1]}
    if whoseTurn == 1: #1 for player, 0 for computer
        rowChoice[move] = playerMarker #say playerMarker = 'X'
        print ("rowChoice[move] is ", rowChoice[move]) #for checking
        whoseTurn -= 1
    elif whoseTurn == 0:
        rowChoice[move] = comMarker
        print ("Com's turn")
        whoseTurn += 1
    print (''.join(row1))
    print ("-+-+-")
    print (''.join(row2))
    print ("-+-+-")
    print (''.join(row3))
    return row1, row2, row3, whoseTurn

row1, row2, row3, playerTurn = displayBoard (new_row1, new_row2, new_row3, playerMarker,
                                                 comMarker, playerTurn, move)
Hi guys,

Am trying to put the lists row1 as the values of a dictionary rowChoice,
for example playerMarker is 'X' and move is '7', am trying to pass the value of playerMarker -> rowChoice[7] (rowChoice[move]) -> row1[0] -> row1.

I am able to assign rowChoice[7] the value, but not row1[0] and row1. Please assist. Thanks
Reply
#2
I don't understand what you are trying to do. At all. The usual way to use a dictionary for a tic tac toe game is to number the squares from zero through 8, or 1 through 9, so those are the keys of a dictionary. The user selects a square number and the dictionary is updated with an X or O for that key (square number) . You can then check squares 1 ,2, 3 (and all of the other winning combinations) to see if they are all equal.
.
Reply
#3
new_row1 = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']

def displayBoard(row1, playerMarker, comMarker, whoseTurn, move):

    if whoseTurn == 1: #1 for player, 0 for computer
        print ("Player making move at ", (move))
        row1[(move-1)] = playerMarker
        whoseTurn -= 1
    elif whoseTurn == 0:
        print ("Computer making move at ", (move-1))
        row1[(move-1)] = comMarker
        whoseTurn += 1
    print (row1[6] + '|' + row1[7] + '|' + row1[8])
    print ("-+-+-")
    print (row1[3] + '|' + row1[4] + '|' + row1[5])
    print ("-+-+-")
    print (row1[0] + '|' + row1[1] + '|' + row1[2])
    return row1, whoseTurn

def playersTurn(row1):
    while True:
        print ("What is your next move? (1-9)")
        move = int(input())
        if row1[(move-1)] != ' ':
            print ("The move has been occupied")
        elif move in range (1,10):
            return move
        else:
            print ("Please key in 1-9 in numeric value only.")

while True:
    move = playersTurn(new_row1)
    row1, playerTurn = displayBoard (new_row1, playerMarker, comMarker, playerTurn, move)
Hi, I managed to achieve what I intend to do with the code as shown above. However, I am wondering if I could achieve the same function using dictionary
    rowChoice = {7: row1[0], 8: row1[2], 9: row1[4],
                 4: row2[0], 5: row2[2], 6: row2[4],
                 1: row3[0], 2: row3[2], 3: row3[4], 10: row1[1]}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 495 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 597 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,879 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,900 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,535 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,524 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,883 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Python dictionary with values as list to CSV Sritej26 4 2,958 Mar-27-2021, 05:53 PM
Last Post: Sritej26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020