# Untitled

unknown

python

a year ago

5.4 kB

4

Indexable

Never

^{}

import random board = [["","",""],["","",""],["","",""]]; def print_board(i,j): print("---------"); print_board_recursive(i,j); print("---------"); aux = ""; def print_board_recursive (i,j): global aux; if(i < len(board)): if(j < len(board)): aux = aux + board[i][j] + "\t"; print_board_recursive(i, j + 1); else: if(i < len(board) - 1): print(aux + "\n"); else: print(aux); aux = ""; print_board_recursive(i + 1, 0); else: return; def start (): print("rules: ") print(" You would have to choose if the pieces in a 3x3 board, move like the clockwise or counterclockwise"); print(" The distribution and number of pieces are random"); print(" There can not be more than 5 pieces"); print(" There can not be pieces in the middle space"); print(" If the next place for a piece is occupied, the piece does not move"); print("to stop the program, you have to answer stop"); number_of_pieces = random.randint(2,5); place_pieces(number_of_pieces,0); move_pieces(number_of_pieces); def place_pieces (number_of_pieces, pieces_counter): if(pieces_counter == number_of_pieces): return; x = random.randint(0,2); y = random.randint(0,2); if(board[x][y] != "X"): if(x != 1 or y != 1): board[x][y] = "X"; place_pieces(number_of_pieces, pieces_counter + 1); else: place_pieces(number_of_pieces, pieces_counter); else: place_pieces(number_of_pieces, pieces_counter); def move_pieces(number_of_pieces): direction = input("Do you want the pieces to move clockwise or counterclockwise? "); if(direction == "stop"): return; else: print_board(0,0); choose_pieces_recursive(number_of_pieces, 0, 0, 0, direction); move_pieces(number_of_pieces); def choose_pieces_recursive(number_of_pieces, pieces_counter, row, column, direction): counter_pieces = pieces_counter if(pieces_counter == number_of_pieces): return; if(board[row][column] == "X"): counter_pieces = counter_pieces + 1; move_pieces_effective(row, column, direction); if(direction == "clockwise"): if(column == 0 and row < 2): choose_pieces_recursive(number_of_pieces, counter_pieces, row + 1, column, direction); elif(row == 0 and column > 0): choose_pieces_recursive(number_of_pieces, counter_pieces, row, column - 1, direction); elif(column == 2 and row > 0): choose_pieces_recursive(number_of_pieces, counter_pieces, row - 1, column, direction); elif(row == 2 and column < 2): choose_pieces_recursive(number_of_pieces, counter_pieces, row, column + 1, direction); elif(direction == "counterclockwise"): if(row == 0 and column < 2): choose_pieces_recursive(number_of_pieces, counter_pieces, row, column + 1, direction); elif(column == 2 and row < 2): choose_pieces_recursive(number_of_pieces, counter_pieces, row + 1, column, direction); elif(column == 0 and row > 0): choose_pieces_recursive(number_of_pieces, counter_pieces, row - 1, column, direction); elif(row == 2 and column > 0): choose_pieces_recursive(number_of_pieces, counter_pieces, row, column - 1, direction); def move_pieces_effective(row, column, direction): if(direction == "clockwise"): if(row == 0 and column < 2): if(board[row][column + 1] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row][column + 1] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(column == 2 and row < 2): if(board[row + 1][column] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row + 1][column] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(column == 0 and row > 0): if(board[row - 1][column] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row - 1][column] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(row == 2 and column > 0): if(board[row][column - 1] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row][column - 1] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(direction == "counterclockwise"): if(column == 0 and row < 2): if(board[row + 1][column] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row + 1][column] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(row == 0 and column > 0): if(board[row][column - 1] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row][column - 1] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(column == 2 and row > 0): if(board[row - 1][column] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row - 1][column] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); elif(row == 2 and column < 2): if(board[row][column + 1] == "X"): print_board(0,0); print("no moves possible"); print("\n"); else: board[row][column + 1] = "X"; board[row][column] = ""; print_board(0,0); print("\n"); start()