Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discussion 2: Intro to Project 1: Chess Submit Project 1 under # 11!

Similar presentations


Presentation on theme: "Discussion 2: Intro to Project 1: Chess Submit Project 1 under # 11!"— Presentation transcript:

1 Discussion 2: Intro to Project 1: Chess Submit Project 1 under # 11!

2 “Better” Emacs for Windows Download Xming from http://sourceforge.net/projects/xming/http://sourceforge.net/projects/xming/ Run Xming. There should be a icon in the bottom right if it is running. Open Putty. On the left, go to SSH -> X11. Click on Enable X11 forwarding Login as normal.

3 “Better” Emacs for Macs Download and run Xquartz / X11 from https://xquartz.macosforge.org/trac https://xquartz.macosforge.org/trac Open a X11 terminal. Ssh as normal using “ssh –X username@linux.glue.umd.edu”username@linux.glue.umd.edu Enter password When you type “emacs cprogram.c,” a better emacs will appear!

4 “Better” Emacs Tips Type “emacs cprogram.c &” in order to be able to use the terminal with emacs open Go to Options and check C-x,C-c,C-v cut copy paste in order to use cut copy paste by hitting Ctrl-X, Ctrl –C, Ctrl-V respectively

5 Testing Projects You will be given input files (test1.input) and output files (test1.output) for this project. Compile your program using “gcc check.c chess.c board.c” Type “a.out ! output.txt” test1.input is the input. output.txt is the answer from your program test1.output is the expected answer. Type diff output.txt test1.output This will compare your answer with the expected answer. If nothing shows up, this is good. If something shows up, FIX YOUR FORMATTING/PROGRAM to get nothing to show up Not testing this way is a great way to lose LOTS of points even if you have a working program!

6 Wget Type “wget http://www.ece.umd.edu/class/enee150.F2015/assignments/pr1/chec k.c” to get the check.c file http://www.ece.umd.edu/class/enee150.F2015/assignments/pr1/chec k.c Similarly, replace check.c with chess.h, board.c and chess.c in order to get all the files for this project

7 Chess Overview Taking turns, the White player and Black player will enter moves (x_from, y_from, x_to, y_to) If the move is valid, the move is made If the move is invalid, the player will continuously enter moves until a valid move is given Enter -1, -1, -1, -1 to exit

8 Chess Overview: Board The array for the board has Y first!!! Suppose I made the move shown with the blue arrow: Entered as xfrom, yfrom, xto, yto. Entered as 0, 2, 2, 4 When you access the array for the FROM value, it’d be array[yfrom][xfrom] When you access the array fro the TO value, it’d be array[yto][xto] X, 0-7 Y, 0-7 0 07 7

9 Chess Overview: Givens chess.h – contains many function declarations and the two board_piece and board_color arrays, along with many #defined constants board.c – declares the board_piece and board_color arrays and has the print_board function chess.c – the main function is here. It has a loop that prints the board, calls get_valid_move for each player (alternating), calls check_move to see whether the move is valid. If the move is invalid, it keeps asking the player for a valid move. If the move is valid, it makes the move. check.c – contains function to check whether moves are valid or invalid. (You will only be changing this.c file)

10 Your Job The chess structure is already made! DO NOT CHANGE chess.c, chess.h or board.c So, you do not have to worry about making the move, user input, ending the program, etc. You are only going to write/change 7 functions in check.c, which will be used to check whether a move is valid. At this point (with no changes whatsoever), you could compile the board.c, chess.c and check.c and it would play chess. However, any input you enter will be considered VALID, so it’s not useful

11 check.c Only thing you have to write is the 7 functions Has 7 functions used to check whether a given move is valid chess.c calls check_move(int color, int x_from, int y_from, int x_to, int y_to) check_move performs general checks and piece specific checks and returns VALID or INVALID (The return value of check_move affects the game, so you want to make sure it’s correct).

12 The 7 Functions You are required to write the following functions in check.c check_move check_pawn_move check_rook_move check_knight_move check_bishop_move check_queen_move check_king_move Same parameters: (int color, int x_from, int y_from, int x_to, int y_to)

13 check_move check_move performs general checks and piece specific checks and returns VALID or INVALID General Checks: (return INVALID if not true) xfrom, xto, yfrom, yto are all between 0 – 7 inclusive. (If this is NOT true, return INVALID immediately to avoid accessing invalid array values) The from square and to squares are different (you are not trying to move a piece into its original place) The from square has a piece of the color of the turn player (you are not trying to move an opponent’s piece/move nothing) The to square has a piece of the opposing color OR is empty. (you are not trying to capture your own piece) Piece Specific Checks (check whether the piece’s movement is correct) If the General Checks are valid, call one of the 6 functions to corresponding to the piece that is to be moved. Example: if a King is being moved, call check_king_move. If it is all VALID, return VALID. Otherwise, return INVALID.

14 Piece Specific Checks Checks whether specific moves are valid for a certain piece. Ex: Pawns move differently from bishops, so we need to check whether a pawn move is valid differently from bishops Returns VALID or INVALID to check_move check_pawn_move check_rook_move check_knight_move check_bishop_move check_queen_move check_king_move

15 check_pawn_move Let xdiff = xto-xfrom. Let ydiff = yto-yfrom For a pawn, the following moves are possible: For White (valid moves) If xdiff is 0 and ydiff is 1, it is always valid (moving forward) If xdiff is 0 and ydiff is 2 and it’s in the starting row (moving forward in the beginning) If xdiff is 1 or -1, ydiff must be 1 AND there must be an opponent’s piece at (xfrom,yfrom) (capturing) Similar idea for black, but in the opposite direction (up instead of down)

16 check_rook_move A rook valid move must have xdiff = 0 OR ydiff = 0 (but not both) Why? You should figure out which direction the move is in (left, right, up or down). Also, there should be no obstructions between the rook’s move. Accomplished using a loop: Start at from, go to to, moving in the direction of the move (left, right, up, down) 1 square at time. This is because a rook cannot jump over another piece.

17 check_bishop_move A bishop valid move is when xdiff = ydiff or xdiff = -ydiff. Why? There are 4 possible directions a bishop can move diagonally (+1,+1),(+1,-1),(-1,+1), (-1,-1) Like the rook, you have to use a loop to make sure there are no obstructions, but this time moving diagonally

18 check_queen_move A queen is a rook and bishop combined, so just use check_rook_move and check_bishop_move and CHECK BOTH

19 check_king_move A king is a queen that can only move 1 square in any direction. Essentially check_queen_move, but xdiff has to be -1,0,1. Likewise, ydiff has to be -1,0,1.

20 check_knight_move Knights can jump over other pieces in an L shape Each of the 8 L shapes is 2 in one X-Y direction and 1 in the other Check whether xdiff = +- 2 and ydiff = +-1 Check whether xdiff = +- 1 and ydiff = +-2

21 Special Rules in Chess None of the following is VALID/needs to be implemented Pawn promotion Castling Check/Checkmate (this means the king can be taken and the game will continue) En passant

22 Grading 10% style 10% compiles 80% Testing Rank of pieces (do the pieces in this order) Pawn Bishop / Rook Queen King Knight Having a working Bishop, Rook, Pawn is 70-80% of the testing

23 Style Use good indentation Use good variable names Comment! At least one comment in front of each function describing how it works.


Download ppt "Discussion 2: Intro to Project 1: Chess Submit Project 1 under # 11!"

Similar presentations


Ads by Google