0

I'm currently making a "match stick" game in CLI and It's Player vs AI. Almost everything works properly except that the "AI" chooses a stick that has been previously removed.

Here is the code:

class CPyramide(object):
    def __init__(self, lines):
        self.pir = []
        for i in range(lines):
            sticks = ["|" for j in range(i+1)]
            self.pir.append(sticks)
    def __str__(self):
        o = ""
        for i, L in enumerate(self.pir):
            spaces = (len(self.pir) - i) * " "
            o += spaces
            o += " ".join(L)
            o += "\n"
        return o

    def stickDelete(self, line, n):
        self.pir[line] = self.pir[line][n:]

try:
    lines = int(sys.argv[1])
    sticks = int(sys.argv[2])
    cpir = CPyramide(lines)
    print(cpir)
    while True:
        print("Your turn:")
        inputLine = input("Line: ")
        inputSticks = input("Matches: ")
        if int(inputSticks) > sticks:
            print("Error: you cannot remove more than",sticks,"matches per turn")
            continue
        else:
            print("Player removed",inputSticks,"match(es) from line",inputLine)
            cpir.stickDelete(int(inputLine) - 1,int(inputSticks))
            print(cpir)
            print("AI's turn...")
            aiSticks = randint(1,sticks)
            aiLines = randint(1,lines)
            print("AI removed",aiSticks,"match(es) from line",aiLines)
            cpir.stickDelete(aiLines - 1,aiSticks)
            print(cpir)

I've been trying to make it so it checks every array that contains a [|] and possibly remove it but I don't know how I can make it.

Is there a way I can make the function cpir.stickDelete checks if an array has a [|] in it and possibly remove it but randomly? Because everytime I'm playing with the AI it just chooses something that has already been previously removed.

Is there a way to check through every array and possibly check if it contains a [|] and remove it but randomly?

Thanks for reading.

  • 7
    Please, try to create a [minimal, complete and verifiable example](https://stackoverflow.com/help/mcve) that we can work with, instead of the entirety of your code. You can check if an object is in a container with the `in` operator (e.g. `if some_thing in container: do_something()`) – jdehesa Feb 05 '19 at 16:44
  • 1
    To answer the title: `my_list.count('|')` will give you an answer to "check if list has something in it" by returning the ammount of that thing. What jdehesa said if you just need to know *if* there is one or more of that item. And [this](https://stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-list) will give you an idea how to remove every occurrence of `X` item in a list. – Torxed Feb 05 '19 at 16:49
  • Using list(filter((["|"]).__ne__, self.pir[line])) doesn't work I also tried count() – Michael McCain Feb 05 '19 at 16:58

1 Answers1

0

Try this :

if "|" in my_list:
    my_list.remove("|")
Fukiyel
  • 1,166
  • 7
  • 19