Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Draughts
#1
The program keeps resetting the Team and counter variables stopping the y team from playing.
 
import os
import sys

X_Team_lives = 12
Y_Team_lives = 12



board = []

for x in range(0, 8):
  board.append(["O"] * 8)

  # setting up team x
board[7][0] ="x"
board[5][0] ="x"
board[6][1] ="x"
board[7][2] ="x"
board[5][2] ="x"
board[6][3] ="x"
board[7][4] ="x"
board[5][4] ="x"
board[6][5] ="x"
board[7][6] ="x"
board[5][6] ="x"
board[6][7] ="x"

# setting up team y
board[1][0] ="y"
board[0][1] ="y"
board[2][1] ="y"
board[1][2] ="y"
board[0][3] ="y"
board[2][3] ="y"
board[1][4] ="y"
board[0][5] ="y"
board[2][5] ="y"
board[1][6] ="y"
board[0][7] ="y"
board[2][7] ="y"

def print_board(board):
  for row in board:
    print " ".join(row)


def Menu(board):
  
  os.system('cls')
  print " "
  print "Draughts"
  print " "
  print "by Matthew John Steel"
  print " "
  print " "
  print "Start game (S)"
  print "Rules (R)"
  print "Exit (X)"
  input = raw_input("What do you want to do?: ")
  if input == "S" or input == "s":
    Team ="y"
    Game(board, X_Team_lives, Y_Team_lives,Team)
  elif input == "R" or input == "r":
     Rules(board)
  elif input == "X" or input == "x":
    sys.exit()

def Rules(board):
  print " "
  print_board(board)
  print " "
  print "Starting position - The starting setup; X moves first. Each player starts with 12 men on the dark squares of the three rows closest to that player's side (see diagram). The row closest to each player is called the kings row or crownhead. The player with the x pieces moves first. Move rules-There are two different ways to move in English draughts: Simple move: A simple move consists of moving a piece one square diagonally to an adjacent unoccupied dark square. Uncrowned pieces(x,y) can move diagonally forward only; kings can move in any diagonal direction. Jump: A jump consists of moving a piece that is diagonally adjacent an opponent's piece, to an empty square immediately beyond it in the same direction. (Thus 'jumping over' the opponent's piece.) Men can jump diagonally forward only; kings can jump in any diagonal direction. A jumped piece is considered 'captured' and removed from the game. Any piece, king or man, can jump a king. Multiple jumps are possible, if after one jump, another piece is immediately eligible to be jumped—even if that jump is in a different diagonal direction. If more than one multi-jump is available, the player can choose which piece to jump with, and which sequence of jumps to make. The sequence chosen is not required to be the one that maximizes the number of jumps in the turn; however, a player must make all available jumps in the sequence chosen. Kings(X,Y)If a man moves into the kings row on the opponent's side of the board, it is crowned as a king and gains the ability to move both forward and backward. If a man jumps into the kings row, the current move terminates; the piece is crowned as a king but cannot jump back out as in a multi-jump, until the next move.End of game-A player wins by capturing all of the opponent's pieces."
  print " "
  input = raw_input("press enter to return to menu")
  print " "
  Menu(board)


def Game(board, X_Team_lives, Y_Team_lives, Team):
  counter = 0
  # so x starts
  while X_Team_lives != 0 or Y_Team_lives != 0:
    turn(board, counter, Team)
  if X_Team_lives == 0 or Y_Team_lives == 0:
      print "%s is the winner" %Team
      
  Menu(board)

def turn(board, counter,Team):
  counter = counter + 1
  os.system('cls')
  print "Turn %s" %(counter)
  if Team == "x":
    Team = "y"
  elif Team == "y":
    Team = "x"
  print " "
  print_board(board)
  print " "
  print "Your turn %s"  %(Team)
  print " "
  print "X- %s  Y- %s" %(X_Team_lives, Y_Team_lives)
  current_row = int(raw_input("Enter Row of Piece you want to move: "))
  current_col = int(raw_input("Enter Column of Piece you want to move: "))
  
  correct_row = False
  correct_col = False
  
  move(board,current_row,current_col, Team,correct_row,correct_col)



def move(board,current_row,current_col, Team,correct_row,correct_col):
  Piece = board[current_row][current_col]
# What pieces can move is determined by the current team
  if Team == "x":
    if Piece == "x":
      NewRow = int(raw_input("Enter Row you want to move to : "))
      NewCol = int(raw_input("Enter Column you want to move to: "))
      Row_movement = NewRow - current_row
      Col_movement = NewCol - current_col

      if Row_movement <> -1 or Col_movement <> 1 or Col_movement <> -1:
        while correct_row == False and correct_col == False:
          print " Enter a viable move"
          current_row = int(raw_input("Enter Row of Piece you want to move: "))
          current_col = int(raw_input("Enter Column of Piece you want to move: "))
          NewRow = int(raw_input("Enter Row you want to move to : "))
          NewCol = int(raw_input("Enter Column you want to move to: "))
          Row_movement = NewRow - current_row
          Col_movement = NewCol - current_col
          if Row_movement == -1:
           correct_row = True
          if Col_movement == 1 or Col_movement == -1:
            correct_col = True
          
          
      if board[NewRow][NewCol] == "x" or board[NewRow][NewCol] == "X":
        print "one of your Pieces are already here! move somewhere else! Pick a new piece"
        current_row = int(raw_input("Enter Row of Piece you want to move: "))
        current_col = int(raw_input("Enter Column of Piece you want to move: "))
  
      elif board[NewRow][NewCol] == "y" or board[NewRow][NewCol] == "Y":
        take_Piece(current_row,current_col,NewRow,NewCol)
      else:
        board[NewRow][NewCol] = "x"
        board[current_row][current_col] = "O"
      if NewRow == 0:
        board[NewRow][NewCol] = "X"
    
    if Piece == "X":
      NewRow = int(raw_input("Enter Row you want to move to : "))
      NewCol = int(raw_input("Enter Column you want to move to: "))
      Row_movement = NewRow - current_row
      Col_movement = NewCol - current_col
      if Row_movement != 1 or Row_movement != -1 or Col_movement != 1 or Col_movement != -1:
        if Row_movement <> -1 or Col_movement <> 1 or Col_movement <> -1:
          NewRow = int(raw_input("Enter Row you want to move to : "))
          NewCol = int(raw_input("Enter Column you want to move to: "))
          Row_movement = NewRow - current_row
          Col_movement = NewCol - current_col
          if Row_movement == 1 or Row_movement == -1:
           correct_row = True
          if Col_movement == 1 or Col_movement == -1:
           correct_col = True
          
      if board[NewRow][NewCol] == "x" or board[NewRow][NewCol] == "X":
        print "one of your Pieces are already here! move somewhere else"
      elif board[NewRow][NewCol] == "y" or board[NewRow][NewCol] == "Y":
        take_Piece(current_row,current_col,NewRow,NewCol)
      else:
        board[NewRow][NewCol] = "X"
        board[current_row][current_col] = "O"
      

  else:
      if Piece == "y":
        NewRow = int(raw_input("Enter Row you want to move to : "))
        NewCol = int(raw_input("Enter Column you want to move to: "))
        Row_movement = NewRow - current_row
        Col_movement = NewCol - current_col

        if Row_movement <> 1 or Col_movement <> 1 or Col_movement <> -1:
          if Row_movement <> -1 or Col_movement <> 1 or Col_movement <> -1:
            print " Enter a viable move"
            current_row = int(raw_input("Enter Row of Piece you want to move: "))
            current_col = int(raw_input("Enter Column of Piece you want to move: "))
            NewRow = int(raw_input("Enter Row you want to move to : "))
            NewCol = int(raw_input("Enter Column you want to move to: "))
            Row_movement = NewRow - current_row
            Col_movement = NewCol - current_col
            if Row_movement == 1:
              correct_row = True
            if Col_movement == 1 or Col_movement == -1:
              correct_col = True

          if board[NewRow][NewCol] == "y" or board[NewRow][NewCol] == "Y":
            print "one of your Pieces are already here! move somewhere else"

          elif board[NewRow][NewCol] == "x" or board[NewRow][NewCol] == "X":
            take_Piece(current_row,current_col,NewRow,NewCol)
          else:
            board[NewRow][NewCol] = "y"
            board[current_row][current_col] = "O"
          if NewRow == 7:
             board[NewRow][NewCol] = "Y"

      elif Piece == "Y":
        NewRow = int(raw_input("Enter Row you want to move to : "))
        NewCol = int(raw_input("Enter Column you want to move to: "))
        Row_movement = NewRow - current_row
        Col_movement = NewCol - current_col
        if Row_movement != 1 or Row_movement != -1 or Col_movement != 1 or Col_movement != -1:
          if Row_movement <> -1 or Col_movement <> 1 or Col_movement <> -1:
            NewRow = int(raw_input("Enter Row you want to move to : "))
            NewCol = int(raw_input("Enter Column you want to move to: "))
            Row_movement = NewRow - current_row
            Col_movement = NewCol - current_col
            if Row_movement == 1 or Row_movement == -1:
              correct_row = True
            if Col_movement == 1 or Col_movement == -1:
              correct_col = True
            
        if board[NewRow][NewCol] == "y" or board[NewRow][NewCol] == "Y":
          print "one of your Pieces are already here! move somewhere else"

        elif board[NewRow][NewCol] == "y" or "Y":
          take_Piece(current_row,current_col,NewRow,NewCol)
        else:
          board[NewRow][NewCol] = "Y"
          board[current_row][current_col] = "O"
        


def take_Piece(current_row,current_col,NewRow,NewCol,correct_row,correct_col,Piece,Team):
	
  LoopController = True
  
  if Piece == "x" or Piece == "X":
    EnemyNorm = "y"
    EnemyKing = "Y"
  else:
    EnemyNorm = "x"
    EnemyKing = "X"
#so you cant jump a piece that isn't jump able
  if board[NewRow+correct_row][NewCol+correct_col] != "O":
    print "Can't take that piece as there is a piece in the way, pick a new move."
    move(board,current_row,current_col, Team)
  else: # The jump follows through and takes the piece
    board[NewRow+correct_row][NewCol+correct_col] == Piece
    board[current_row][current_col] == "O"
    board[NewRow][NewCol] == "O"
    current_row = (NewRow+correct_row)
    current_col == (NewCol+correct_col)

  if Team == "X":
    Y_Team_lives = Y_Team_lives - 1
  else:
    X_Team_lives = Y_Team_lives - 1

  if  Piece == "x":
    Direction = -1
  elif Piece == "y":
    Direction = 1

  if Piece == "X" or Piece == "Y":
    while LoopController == True:
      if board[current_row + 1][current_col - 1] == EnemyNorm or board[current_row + 1][current_col - 1] == EnemyKing or board[current_row + 1][current_col + 1] == EnemyNorm or board[current_row + 1][current_col + 1] == EnemyKing:
        Direction = 1
        taken_piece_loop(current_row,current_col,NewRow,NewCol,correct_row,correct_col,Piece,Team, Direction, EnemyKing, EnemyNorm)


      elif board[current_row - 1][current_col - 1] == EnemyNorm or board[current_row - 1][current_col - 1] == EnemyKing or board[current_row - 1][current_col + 1] == EnemyNorm or board[current_row - 1][current_col + 1] == EnemyKing:
        Direction = -1
        taken_piece_loop(current_row,current_col,NewRow,NewCol,correct_row,correct_col,Piece,Team, Direction, EnemyKing, EnemyNorm)


      else:
        os.system('cls')
        LoopController = False
        print " "
        print_board(board)
        print " "
        print "X- " & X_Team_lives & " Y- " & Y_Team_lives
        print " "
        

  else:
	  while LoopController == True:
		  if board[current_row + Direction][current_col - 1] == EnemyNorm or board[current_row + Direction][current_col - 1] == EnemyKing or board[current_row + Direction][current_col + 1] == EnemyNorm or board[current_row + Direction][current_col + 1] == EnemyKing:

			  taken_piece_loop(current_row,current_col,NewRow,NewCol,correct_row,correct_col,Piece,Team, Direction, EnemyKing, EnemyNorm)
		  else:
		  	os.system('cls')
		  	print " "
		  	print_board(board)
		  	print " "
		  	print "X- " & X_Team_lives & " Y- " & Y_Team_lives
		  	print " "
		  	move(board,current_row,current_col)




def taken_piece_loop(current_row,current_col,NewRow,NewCol,correct_row,correct_col,Piece,Team, Direction, EnemyKing, EnemyNorm):
  os.system('cls')
  print_board(board)
  print "X- " & X_Team_lives & " Y- " & Y_Team_lives
  if board[current_row + Direction][current_col - 1] == EnemyNorm and board[current_row + Direction][current_col + 1] == EnemyNorm:
    print "Pick- (1)" & [current_row + Direction][current_col - 1] & " (2)" & board[current_row + Direction][current_col + 1]
    pick = int(raw_input("pick number : "))
    if pick == 1:
      col_direction = -1
    elif pick == 2:
      col_direction = 1

  elif board[current_row + Direction][current_col - 1] == EnemyKing and board[current_row + Direction][current_col + 1] == EnemyNorm :
    print "Pick- (1)" & [current_row + Direction][current_col - 1] & " (2)" & board[current_row + Direction][current_col + 1]
    pick = int(raw_input("pick number : "))
    if pick == 1:
      col_direction = -1
    elif pick == 2:
      col_direction = 1

  elif board[current_row + Direction][current_col - 1] == EnemyNorm and board[current_row + Direction][current_col + 1] == EnemyKing:
    print "Pick- (1)" & [current_row + Direction][current_col - 1] & " (2)" & board[current_row + Direction][current_col + 1]
    pick = int(raw_input("pick number : "))
    if pick == 1:
      col_direction = -1
    elif pick == 2:
      col_direction = 1

  elif board[current_row + Direction][current_col - 1] == EnemyKing and board[current_row + Direction][current_col + 1] == EnemyKing:
    print "Pick- (1)" & [current_row + Direction][current_col - 1] & " (2)" & board[current_row + Direction][current_col + 1]
    pick = int(raw_input("pick number : "))
    if pick == 1:
      col_direction = -1
    elif pick == 2:
      col_direction = 1

  elif board[current_row + Direction][current_col - 1] == EnemyKing or board[current_row + Direction][current_col - 1] == EnemyKing:
    col_direction = -1

  elif board[current_row + Direction][current_col + 1] == EnemyKing or board[current_row + Direction][current_col + 1] == EnemyKing:
    col_direction = 1

  board[current_row + 2*Direction][correct_col + 2*col_direction] == Piece
  board[current_row][current_col] == "O" and board[current_row + Direction][correct_col + col_direction] == "O"
  current_row = (current_row + 2*Direction)
  current_col == (correct_col + 2*col_direction)


  if Team == "x":
    Y_Team_lives = Y_Team_lives - 1
  else:
    X_Team_lives = Y_Team_lives - 1

    os.system('cls')
    print " "
    print_board(board)
    print " "
    print "X- " & X_Team_lives & " Y- " & Y_Team_lives

Menu(board)
Reply
#2
try to provide minimal runnable example that reproduce the error/problem, remove need for user input in it, try to provide relevant information where you think the problem is. Hardly someone will look into 364 lines of non-pythonic code, that doesn't have any comments or explanation.
Reply
#3
(Mar-08-2018, 11:09 AM)buran Wrote: try to provide minimal runnable example that reproduce the error/problem, remove need for user input in it, try to provide relevant information where you think the problem is. Hardly someone will look into 364 lines of non-pythonic code, that doesn't have any comments or explanation.
Sorry I put this up without really thinking about that. I didn't really think.

The thing is I dont know what exacly is stopping "Team" and "counter" from changing despite "board" changing, I put the whole thing up hoping that someone can see something I can't. I do apologise for the size but I don't know what to take out.
Reply


Forum Jump:

User Panel Messages

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