1

I’m attempting to make a game called nine men’s morris in python, and I can’t figure out why the string won’t replace when conditions are satisfied the next time the gameplay_board() function is called. anyone have any ideas?

I was expecting the value of a1 to be changed from ' ' to , but it doesn’t seem to work.

#ring 1
a1="1"
b1="2"
c1="3"
d1="4"
e1="5"
f1="6"
g1="7"
h1="8"

#ring 2
a2="1"
b2="2"
c2="3"
d2="4"
e2="5"
f2="6"
g2="7"
h2="8"

#ring 3
a3="1"
b3="2"
c3="3"
d3="4"
e3="5"
f3="6"
g3="7"
h3="8"

def example_board():
    global a1, b1, c1, d1, e1, f1, g1, h1, a2, b2, c2, d2, e2, f2, g2, h2, a3, b3, c3, d3, e3, f3, g3, h3
    print("\n" + 
    a1 + "----------" + b1 + "----------" + c1 + "\n" + 
    "|          |          |" + "<---Ring 1" + "\n" + 
    "|  " + a2 + "-------" + b2 + "-------" + c2 + "  |" + "\n" + 
    "|  |       |       |<---Ring 2" + "\n" + 
    "|  |  " + a3 + "----" + b3 + "----" + c3 + "  |  |" + "\n" +
    "|  |  |         |<---Ring 3" + "\n" +
    d1 + "--" + d2 + "--" + d3 + "         " + e3 + "--" + e2 + "--" + e1 + "\n" + 
    "|  |  |         |  |  |"  + "\n" + 
    "|  |  " + f3 + "----" + g3 + "----" + h3 + "  |  |" + "\n" +
    "|  |       |       |  |" + "\n" + 
    "|  " + f2 + "-------" + g2 + "-------" + h2 + "  |" + "\n" + 
    "|          |          |" + "\n" +
    f1 + "----------" + g1 + "----------" + h1 + "\n" + "_______________________" + "\n")
    a1=" "
    b1=" "
    c1=" "
    d1=" "
    e1=" "
    f1=" "
    g1=" "
    h1=" "
    a2=" "
    b2=" "
    c2=" "
    d2=" "
    e2=" "
    f2=" "
    g2=" "
    h2=" "
    a3=" "
    b3=" "
    c3=" "
    d3=" "
    e3=" "
    f3=" "
    g3=" "
    h3=" "
example_board()

def gameplay_board(): 
    print("\n" + 
    " " + "----------" + " " + "----------" + " " + "\n" + 
    "|          |          |" + "\n" + 
    "|  " + " " + "-------" + " " + "-------" + " " + "  |" + "\n" + 
    "|  |       |       |  |" + "\n" + 
    "|  |  " + " " + "----" + " " + "----" + " " + "  |  |" + "\n" +
    "|  |  |         |  |  |" + "\n" +
    d1 + "--" + d2 + "--" + d3 + "         " + e3 + "--" + e2 + "--" + e1 + "\n" + 
    "|  |  |         |  |  |"  + "\n" + 
    "|  |  " + f3 + "----" + g3 + "----" + h3 + "  |  |" + "\n" +
    "|  |       |       |  |" + "\n" + 
    "|  " + f2 + "-------" + g2 + "-------" + h2 + "  |" + "\n" + 
    "|          |          |" + "\n" +
    f1 + "----------" + g1 + "----------" + h1 + "\n" + "_______________________" + "\n")

#gameplay_board()
def update(): 
    gameplay_board()
turn=1
bpp=0
wpp=0
def place_pieces(): 
    ringchoice=int(input("Player 1, please select a ring to place your piece on. "))
    if ringchoice==1: #and ringchoice=="one": 
        spotchoice=input("Player 1, please chose a spot to place your piece on. ")
        if spotchoice==1: 
            a1="○"
gameplay_board()
Vasilis G.
  • 7,556
  • 4
  • 19
  • 29
  • 1
    you forgot `global a1` in that function – Matiiss Dec 05 '22 at 23:03
  • 1
    You do not print `a1` in `gameplay_board` function. You print `a1` in `example_board` but you never this function. – Yuri Ginsburg Dec 05 '22 at 23:07
  • This confusion over the use of globals is easily avoided by not using globals -- it'd be much easier to have a single list of lists (i.e. 3 lists of 8 spaces, all in a single list) than to have 24 individual global variables. And because lists are mutable, you don't need to `global` the variable itself to be able to modify the list. – Samwise Dec 05 '22 at 23:17
  • 1
    ANY TIME you find yourself with variables like `xxx1`, `xxx2,`, `xxx3, etc., you need to replace those variables with a list (or sometimes a dict). That will help you here. – Tim Roberts Dec 05 '22 at 23:35
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – relent95 Dec 06 '22 at 00:47

0 Answers0