We can then run Minecraft client. On the loading screen we make a new profile and select 1.4.7 from the version drop-down menu. Using that profile we can connect to our Craftbukkit server using ‘localhost’ as the IP. Leave the server and client running while you write a program.
Python Problem: A Connect Four Board class Background Connect Four is a variation of tic-tac-toe played on a 6x7 rectangular board. The standard board size for Connect Four is six rows of seven columns, but your Board class should be able to handle boards of any dimensions. However, for simplicity we will preserve the four-in-a-row requirement for winning the game regardless of the board size (even for boards with dimensions less than 4x4). Write a constructor init(self, height, width) that constructs a new Boardobject by initializing the following three attributes: an attribute height that stores the number of rows in the board, as specified by the parameter height an attribute width that stores the number of columns in the board, as specified by the parameter width an attribute slots that stores a reference to a two-dimensional list with height rows and width columns that will be used to store the current contents of the board.
Each slot will contain one of the following characters: a space character, ' ', to represent an empty slot. An uppercase X character, 'X', to represent a checker from one of the two players. An uppercase O character, 'O', to represent a checker from the other player. Be careful that you consistently use an uppercase O for this purpose, and not the zero ('0') character. The board is initially empty, so all of the slots should initially contain a space character, ' '. To ensure that you are creating separate independent rows (as opposed to copies of the reference to the same row), an approach is to use a list comprehension: self.slots = ' '. self.width for row in range(self.height) B.
Write a method repr(self) that returns a string representing a Board object. Each slot should take up one space, and all columns should be separated by vertical bar (' ') characters. Additionally, the columns should be labeled at the bottom with a column number. Here is an example for an empty 6x7 board: b = Board(6, 7) b - 0 1 2 3 4 5 6 In order to keep the column numbers in line, the numbering should be done modulo ten, as this larger (5x15) example shows: b2 = Board(5, 15) b2 - 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 add the code needed to include the hyphen characters (-) at the bottom of the board and the column numbers beneath it.
Def repr(self): ' Returns a string representation for a Board object. ' s = ' # begin with an empty string # add one row of slots at a time for row in range(self.height): s += ' ' # one vertical bar at the start of the row for col in range(self.width): s += self.slotsrowcol + ' ' s += ' n' # newline at the end of the row # Add code here for the hyphens at the bottom of the board # and the numbers underneath it. Write a method addchecker(self, checker, col) that accepts two inputs: checker, a one-character string that specifies the checker to add to the board (either'X' or 'O'). Col, an integer that specifies the index of the column to which the checkershould be added and that adds checker to the appropriate row in column col of the board. We encourage you to begin your addchecker method as follows: def addchecker(self, checker, col): ' put your docstring here ' assert(checker 'X' or checker 'O') assert(0 b = Board(6, 7) b.addchecker('X', 0) b.addchecker('O', 0) b.addchecker('X', 0) b.addchecker('O', 3) b.addchecker('O', 4) # cheat and let O go again! b.addchecker('O', 5) b.addchecker('O', 6) b X O X O O O O - 0 1 2 3 4 5 6 D. Write a method reset(self) that should reset the Board object on which it is called by setting all slots to contain a space character.
Hint: There are two ways of writing this method. One way involves looping over all slots of the board to set them to a space character. Can you think of a simpler way? Copy and paste the following method into your Board class, ensuring that proper indentation is preserved: def addcheckers(self, colnums): ' takes in a string of column numbers and places alternating checkers in those columns of the called Board object, starting with 'X'. ' checker = 'X' # start by playing 'X' for colstr in colnums: col = int(colstr) if 0 b = Board(3, 3) b.addcheckers('0200') b O X X O - 0 1 2 F.
Write a method canaddto(self, col) that returns True if it is valid to place a checker in the column col on the calling Board object. Otherwise, it should return False.
The method should make sure that col is in the range from 0 to the last column on the board and that the specified column is not full. Examples: b = Board(2, 2) b - 0 1 b.addchecker('X', 0) b.addchecker('O', 0) b O X - 0 1 b.canaddto(-1) False # column number is too low b.canaddto(0) False # column is full b.canaddto(1) True b.canaddto(2) False # column number is too high G.
Write a method isfull(self) that returns True if the called Board object is completely full of checkers, and returns False otherwise. Hint: You may find it helpful to use the canaddto method that you wrote above. Examples: b = Board(2, 2) b.isfull False b.addcheckers('0011') b O O X X - 0 1 b.isfull True H. Write a method removechecker(self, col) that removes the top checker from column col of the called Board object.
If the column is empty, then the method should do nothing. This method may not seem useful now, but it will become very useful when you implement your own intelligent Connect Four player! Examples: b = Board(2, 2) b.addcheckers('0011') b.removechecker(1) b.removechecker(1) b.removechecker(1) # column empty; should have no effect b.removechecker(0) b X - 0 1 We also encourage you to try printing or evaluating the board after each of the individual calls to removechecker. Write a method iswinfor(self, checker) that accepts a parameter checker that is either 'X' or 'O', and returns True if there are four consecutive slots containing checkeron the board. Otherwise, it should return False. Remember that a win in Connect Four occurs when there are four consecutive checkers of the same type either horizontally, vertically, or diagonally. Moreoever, there are twodiagonal orientations: going “up” from left to right, and going “down” from left to right.
Suggested Approach One way to approach this method is to consider each possible anchor checker that could start a four-in-a-row run. For example, all of the “anchors” that could start a horizontal run from left to right must be in a column at least four slots away from the end of the board. This constraint will help you avoid out-of-bounds errors. Here is some starter code that illustrates this technique: def ishorizontalwin(self, checker): ' Checks for a horizontal win for the specified checker. ' for row in range(self.height): for col in range(self.width - 3): # Check if the next four columns in this row # contain the specified checker. If self.slotsrowcol checker and self.slotsrowcol + 1 checker and self.slotsrowcol + 2 checker and self.slotsrowcol + 3 checker: return True # if we make it here, there were no horizontal wins return False Notes: The backslash characters in the if condition tell Python that the line of code will continue on the next line. The expression self.width - 3 keeps the checking in-bounds, since a horizontal “anchor” cannot begin at or beyond that column.
Testing in different directions will require different guards against going out-of-bounds. We suggest that you create a separate helper function for each of the tests that you need to check to determine if the specified checker has won. We have given youishorizontalwin; now create isverticalwin, isdowndiagonalwin (for diagonals that go down from left to right), and isupdiagonalwin (for diagonals that go up from left to right). Having a separate function for each type of run will make your code easier to test and understand. Once these helper functions are working, haveiswinfor call them to determine what it should return. Hints: Here again, we encourage you to begin your iswinfor function with an assertstatement that validates the input for checker: def iswinfor(self, checker): ' put your docstring here ' assert(checker 'X' or checker 'O') # call the helper functions and use their return values to # determine whether to return True or False We would advise against explicitly counting checkers to see if you reach four, since the four checkers must be adjacent to each other. It’s more convenient to check for all four checkers at once, as we do in ishorizontalwin.
Examples b = Board(6, 7) b.addcheckers('00102030') b O O O O X X X X - 0 1 2 3 4 5 6 b.iswinfor('X') True b.iswinfor('O') True b.reset b.addcheckers('5') b X X X X X O O X O O O - 0 1 2 3 4 5 6 b.iswinfor('X') # up diagonal win True b.iswinfor('O') False.
Hi, I am trying to write a connect four program in python, which I am not very experienced at. Here is how you could write a main loop for your game. Add this to the above post def main: print('Welcome to Connect Four') players = 'OX' playerIndex = 0 while True: # The game's mainloop, we iterate over the players makeboard playerIndex = 1 - playerIndex # alternatively 1 or 0 player = playersplayerIndex # This is 'O' or 'X' move = rawinput('You are%s. Enter a lettered row to make a move!
'% player) while True: # Inner loop: we iterate until valid input move = move.strip # remove trailing white space try: if move 'q': # player entered 'q' to exit raise SystemExit column = 'abcdefg'.index(move) row = findrow(column) break # we exit inner loop except ValueError: move = rawinput('Invalid input, try again: ') pmove(player, row, column) def findrow(column): for i in range(7): if rowsicolumn ' ': return i raise ValueError def pmove(player, row, column): rowsrowcolumn = player main There is still a lot to do. You must check if a player wins and take appropriate action. Ok, i got everything to work.thanks for the help you guys.
Now I need the winning check combos. I spent time figuring out all this but where do I put the function in to check it? You can learn about exceptions here and here. In the above code, I'm using the line column = 'abcdefg'.index(move) This expression returns the position of move in the string 'abcdefg'. For example if move is 'a', it returns 0, if move is 'b', it returns 1, etc. Now suppose that move is 'z'.
The call to index will fail, and it does this by raising an 'exception'. Here the type of this exception is ValueError. If we do nothing, the program exits and prints a 'traceback' like this column = 'abcdefg'.index('z') Traceback (most recent call last): File ', line 1, in ValueError: substring not found What we can do is 'catch' the exception with a try.except statement like this try: column = 'abcdefg'.index(move) except ValueError: print('We didn't compute column, but we caught the exception') The statements in the except part are executed if one of the statements in the try part failed with a ValueError exception. So the program won't crash on this error and we can decide what we want to do when 'move' is not in the string 'abcdefg'.
This is the final code, except the program is not identifying the wincombos as a function. I have tried many things, even rewriting it a few ways, but the program doesn't accept it. I know that it is correct, but what is wrong? Also where do I put it to make it check each time the program ask for the input of the row? Thanks #ConnectFour #Gabe Cohen rows = ' ' for i in range(7) for j in range(7) connect4board = '%s '.
7 def makeboard: for i in range(6, -1, -1): print(('%d '%(i+1)) + (connect4board% tuple(rowsi))) print('-'. 31) print(' ' + (connect4board% tuple('abcdefg'))) def main: print('Welcome to Connect Four!'
) players = 'OX' playerIndex = 0 while True: # The game's mainloop, we iterate over the players makeboard playerIndex = 1 - playerIndex # alternatively 1 or 0 player = playersplayerIndex # This is 'O' or 'X' move = rawinput('You are%s. Enter a lettered row to make a move!