Download presentation
Presentation is loading. Please wait.
1
ENEE150 Discussion 02 Section 0101 Adam Wang
2
Overview More unix commands/logistics Functional Decomposition Project 1
3
X11 Forwarding Allows you to run GUI interfaces from glue
Mac: Xquartz Open server and Ssh –X Windows: Use mobaxterm Otherwise, download xming & run with putty Enable x11 forwarding in putty client Use ‘&’ to run application in the background Ex: emacs board.c &
4
Cat/less/more Displays contents of file
“cat file1” or “less file1” Play around to see the differences in output
5
tar Bundle all your files into one zip
Easier to submit one file Command is “tar –cvf bundle.tar file1 file2 file3” To unzip use “tar –xvf bundle.tar”
6
Wget Download files online into glue
Command example: “wget ece.umd.edu/class/enee150.F2017/assignments/pr1/board.c”
7
Functional decomposition
Making functions is important because Code reuse Code management Abstraction Testing next week probably
8
Project 1: backgammon Due September 21st (two weeks)
9
Rules Move all your pieces into your quadrant, then move them off the board Roll 2 dice to see which moves you can make Can only move to empty spots, spots with your pieces, or spots with one opponent piece
10
The bar If you land on a point with one opponent piece it gets kicked to the bar Opponent has to move pieces off the bar before they can move their other pieces Piece starts off back at the beginning of their respective sides
11
Objective Test Vectors will be given out at a later time
Backgammon.c: controls the game of backgammon Check.c: The 7 functions outlined in the description Basically just how to check whether a move is valid Board.c: Prints and maintains the board Roll_die.c: simulates rolling a dice
12
Do/Don’t Don’t worry about whether the game is actually over; just quit when they type “0 0” Don’t worry about extra moves if they roll a double Don’t have to check whether they can’t make any moves (in this case just quit the entire game) DO check whether they need to make a bar move at the beginning using the_bar_white and the_bar_red DO check whether they can actually make an entry move using num_red and num_white
13
board.c Copy your print_board() function into here
You also need to display the # of pieces on the bar as well now Simple printf statement using the_bar_white and the_bar_red global variables given
14
roll_die.c Create a file called roll_die.c and copy & paste the code in the pdf here Add “int roll_die();” to backgammon.h This simulates a random dice (and we use this for the test vectors as well) If you wanted you could change the roll_die function to return something not random for testing purposes
15
Check.c
16
check.c Create a new C file called check.c for your 7 functions
No need for a main function – will be included in another file Make sure to type #include “backgammon.h” at the top To compile/link everything, just add all the files together i.e. “gcc backgammon.c check.c board.c roll_die.c” Won’t be able to compile everything until all the files are handed out
17
Code reuse is very important
Many of the functions can make use of the other functions to work **Start from the last function listed and work up** Utilize the macros given Compiler goes through and replaces macro names with their values Return VALID_MOVE / return INVALID_MOVE BOARD_SIZE WHITE/RED for mover Makes code more readable/understandable
18
Int check_board_bounds (int index)
Make sure the index is within the bounds of the array Shouldn’t be more than a couple lines of code
19
Int check_die(int mover, int from, int to, int die)
Check that mover is moving exactly the number of spaces on the dice Remember: white can only move up in index, and red can only move down
20
Int check_basic_move(int mover, int from, int to, int die)
Checks validity of a basic piece move Try to start using the other functions to help you here Besides the basic checks, return invalid if there’s no piece at the “from” index, or there are more than one opponent pieces at “to”
21
int check_bear_off_move(int mover, int from, int to, int die)
Checks for bear off moves, i.e. the player moves the pieces off the board Valid as long as our dice roll is high enough to get off the board Don’t forget to check whether there’s a piece to move in the first place
22
Int check_entry_move(int mover, int from, int to, int die)
Check whether is a valid entry move (i.e. moving a piece off the bar) Just have to check whether there’s too many opponent pieces on the space you’re trying to enter on (among other basic checks)
23
Int check_move(int mover, int from, int to, int die)
Put it all together to check validity of a player move ALWAYS think about whether you could use a function to implement something Be sure to check: Whether player has to make an entry move from the bar Whether player is trying to make a bear off move You MUST check whether all their pieces are in the home quadrant Use the num_white/num_red variables Otherwise it’s just a regular move to check
24
backgammon.c
25
backgammon.c Create a new program file for the main game
This will contain your main function (and other helper functions) Use the print_board, roll_die, and check_move functions from your other files here Basically a while loop that switches between the players, and makes the moves/updates the boards until game ends **Up to you how to implement this, but it is recommended to break it up into 3 functions:** Turn Make_move Move_piece
26
Void move_piece (int mover, int from, int to)
Only handles moving a piece Don’t worry about checking whether it’s a valid move here For a normal move, remove a piece from the_board_white/the_board_red[from] and increment the_board_white/the_board_red[to] If moving from the bar, you must decrement the_bar_white/the_bar_red If moving off the board, you must decrement num_white/num_red If they land on an opponent piece, be sure to move it to the bar
27
Int make_move (int mover, int die1, int die2)
Use scanf to process user’s moves, check if valid, then move_piece Use a loop to keep asking user until they enter a valid move Use check_move to check whether valid/invalid! Just print “invalid move” if invalid and ask again If user inputs “0 0” call exit(0); to terminate the program Check which dice causes the valid move, and use it for move_piece If both dice are valid, use the SMALLER dice! Once you call move_piece and print_board, return the other dice
28
void turn (int mover) Roll 2 dice & print results
Call make_move twice (for both dice) Make_move returns the other dice not used, so use that for both dice parameters in your second make_move function
29
Int main() Have a variable that maintains the mover (WHITE/RED)
Start by printing the board Use a while loop to run turn() and to flip between RED and WHITE This loop can run forever; code will terminate from inside make_move()
30
Backgammon.h Put all the functions you used from backgammon.c, roll_die.c, and board.c Use the same format as the required functions already there
31
PLEASE try to use good style
Comment when you can (loops, big blocks of code, functions) We will go over testing once those are handed out too This will be very important because output has to match EXACTLY what is expected Start AT LEAST a week before due date
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.