Creating a console game Super Nim in Python

Greetings to all Khabrovsk residents! I am a student at the Financial University under the Government of the Russian Federation and am studying in the direction of Applied Mathematics and Computer Science. Today I would like to share my tutorial for those learning Python from scratch. The idea for creating this article appeared at a couple of programming workshops, where this task was a project to be submitted. If this tutorial goes well, there will be future releases.

In this lesson we will write a simple console game Super Nim according to the following rules:

On the chessboard, there are pieces or buttons scattered randomly in some squares. Players take turns. In one move, you can remove all the chips from any horizontal or vertical on which they are located. The one who takes the last chips wins.

In our case, we will take the chips/buttons to be ones, and the empty cells will become zeros. Let’s create a chessboard using lists and random:

from random import randint
print('  '+'a',' '+'b',' '+'c',' '+'d',' '+'e',' '+'f',' '+'g',' '+'h')
list = [[],[],[],[],[],[],[],[]]
for el in list:
  for i in range(8):
      el.append(randint(0,1))

In this code, we create a board with randomly assigned zeros and ones, and also output the top coordinates from a to h.

Now let’s create coordinates for the contour lines (from 1 to 8) and associate all the letters and numbers with our fields:

for idx,i in enumerate(list):
  print((chr(ord('1')+idx))+str(i))
x={'a':0,'b':1,'c':2,'d':3,'e':4,'f':5,'g':6,'h':7}
y={'1':0, '2':1, '3':2, '4':3, '5':4, '6':5, '7':6, '8':7}

Let’s try to write the logic for entering coordinates by both players:

arr=[]
def play():
  a=""
  while a!="Игрок 1 победил" and a!="Игрок 2 победил":
      if 1 in list[0] or 1 in list[1] or 1 in list[2] or 1 in list[3] or 1 in list[4] or 1 in list[5] or 1 in list[6] or 1 in list[7]:
        print("\nИгрок 1 вводит координаты")
        letter=input()
        f(letter)
        arr.append(1)
      if 1 in list[0] or 1 in list[1] or 1 in list[2] or 1 in list[3] or 1 in list[4] or 1 in list[5] or 1 in list[6] or 1 in list[7]:
        print("\nИгрок 2 вводит координаты")
        letter=input()
        f(letter)
        arr.append(2)
      else:
        if arr[-1]==1:
          a="Игрок 1 победил"
        else:
          a="Игрок 2 победил"
      print(a)

Here the function will check if there are still units left on the field and output the winner if there are none. If they remain, then the function implements alternating input of coordinates by players. In addition, we will create a kind of “database” in the form of a list that stores the number of the player who entered the coordinates. It is by checking the last element of this list that the winner can be determined. As we can see, the play function calls the f function, which takes the letter entered by the player as an argument. Let’s write it:

def f(letter):
  string_1 = "abcdefgh"
  string_2="12345678"
  if letter in string_1:
    for i in list:
      i[x[letter]]= 0
  if letter in string_2:
    list[y[letter]]=[0]*8
  
  print('  '+'a',' '+'b',' '+'c',' '+'d',' '+'e',' '+'f',' '+'g',' '+'h')
  for idx,i in enumerate(list):
    print((chr(ord('1')+idx))+str(i))

The f function removes all units that are on the selected horizontal or vertical line, and then displays the coordinate field again after updating the values.

After all the code has been written, we can run the play() function and play. Thanks for reading, I hope this tutorial will be useful for beginners!

For those who want to improve their python skills, I suggest doing the following:

  • take the program itself as the second player, which will make a random move

  • try to display the top coordinates using a for loop (like the numbers on the side)

  • shorten the code by getting rid of a lot of if/else

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *