Chess: Difference between revisions

From Computer Science Wiki
Line 17: Line 17:
     row = 0
     row = 0
     for i in range(0,64):
     for i in range(0,64):
         if "(" in board[i]:
         piece = ("[ " + str(board[i]).zfill(2) + " ]")
            print("[" + str(board[i]) + "]"),
         print piece.center(6),    
         else:
            print("[ " + str(i) + " ]")     
         row = row + 1
         row = row + 1
         if row == 8:
         if row == 8:
Line 28: Line 26:


def initialize():
def initialize():
    # black team
     for i in range(8,16):
     for i in range(8,16):
         board[i] = "(p" + str(i-7) + ")"
         board[i] = "p" + str(i-7) + ""
     board[0] = "(r1)"     
     board[0] = "r1"     
     board[7] = "(r2)"  
     board[7] = "r2"  
     board[6] = "(n1)"
     board[6] = "n1"
     board[1] = "(n2)"
     board[1] = "n2"
     board[2] = "(b1)"
     board[2] = "b1"
     board[5] = "(b2)"
     board[5] = "b2"
     board[3] = "(Q)"
     board[3] = "Qu"
     board[4] = "(K)"  
     board[4] = "Ki"  
      
      
     return
     return


 
def move(piece,destination):
    # is piece valid?
    # is destination valid (basic int 1 to 64)
    # is the move valid for that piece?
    # would the move place the king in check?
    return


initialize()  
initialize()  
draw_board()
draw_board()  
player_move = raw_input("What is your move your command should be piece and then square you want to move to. For example, p2 to 18. > ")
piece = player_move[0:2]
destination = player_move[-2:]
move(piece,destination)
</syntaxhighlight>
</syntaxhighlight>



Revision as of 12:16, 22 April 2016

This a problem set for you to work through [1]

This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.

The Problem[edit]

This problem is designed to test your skill and knowledge of programming in python.


Some Code to Get You Started[edit]

# chess
board = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64]

def draw_board():
    row = 0
    for i in range(0,64):
        piece = ("[ " + str(board[i]).zfill(2) + " ]")
        print piece.center(6),    
        row = row + 1
        if row == 8:
            print ("\n")
            row = 0 
    return

def initialize():
    # black team
    for i in range(8,16):
        board[i] = "p" + str(i-7) + ""
    board[0] = "r1"    
    board[7] = "r2" 
    board[6] = "n1"
    board[1] = "n2"
    board[2] = "b1"
    board[5] = "b2"
    board[3] = "Qu"
    board[4] = "Ki" 
    
    return

def move(piece,destination):
    # is piece valid?
    # is destination valid (basic int 1 to 64)
    # is the move valid for that piece?
    # would the move place the king in check?
    return

initialize() 
draw_board() 
player_move = raw_input("What is your move your command should be piece and then square you want to move to. For example, p2 to 18. > ")
piece = player_move[0:2]
destination = player_move[-2:]
move(piece,destination)

Take This Farther[edit]

  • If you manage to create a program to make valid chess moves, start builing a function that makes INTELLIGENT moves.

How you will be assessed[edit]

Every problem set is a formative assignment. Please click here to see how you will be graded

References[edit]

One Possible Solution[edit]

Click the expand link to see one possible solution, but NOT before you have tried and failed!

#
# this function returns a HTML opening and closing pair of tag
#

def make_tags(tag, word):

    print("<" + tag + ">" + word + "</" + tag + ">")

    return

# example input: 
# make_tags('i','Hello world')
 
# example output:
# <i>Hello world</i>