Untitled

mail@pastecode.io avatar
unknown
python
a year ago
9.4 kB
5
Indexable
Never
import random
board = [["","",""],["","",""],["","",""]]
def print_board ():
	print(board[0][0] + " \t" + board[0][1] + " \t" + board[0][2] + "\n");
	print(board[1][0] + " \t" + board[1][1] + " \t" + board[1][2] + "\n");
	print(board[2][0] + " \t" + board[2][1] + " \t" + board[2][2] + "\n");


def start ():
	print("rules: ")
	print("	You would have to choice if the pieces in a 3x3 board, moves like the clockwise or counterclockwise");
	print("	The distribution and amount 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("for 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 and y == 1):
			place_pieces(number_of_pieces, pieces_counter);
		else:
			board[x][y] = "X";
			place_pieces(number_of_pieces, pieces_counter + 1);
	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;
	elif(direction == "clockwise"):
		print_board();
		print("\n");
		move_pieces_clockwise(number_of_pieces,0,0,0)
	elif(direction == "counterclockwise"):	
		print_board();
		print("\n");
		move_pieces_counterclockwise(number_of_pieces,0,0,0)	
	move_pieces(number_of_pieces);


def move_pieces_clockwise(number_of_pieces, counter_pieces, row, column):
	if(counter_pieces == number_of_pieces):
		return;
	if(board[row][column] != "X"):
		if(row == 0):
			if(column == 0):
				move_pieces_clockwise(number_of_pieces, counter_pieces, row + 1, column);
			elif(column == 1):		
				move_pieces_clockwise(number_of_pieces, counter_pieces, row, column - 1);
			else:
				move_pieces_clockwise(number_of_pieces, counter_pieces, row, column - 1);
		elif(row == 1):
			if(column == 0):
				move_pieces_clockwise(number_of_pieces, counter_pieces, row + 1, column);
			else:
				move_pieces_clockwise(number_of_pieces, counter_pieces, row - 1, column);
		else:
			if(column == 0):
				move_pieces_clockwise(number_of_pieces, counter_pieces, row, column + 1);
			elif(column == 1):	
				move_pieces_clockwise(number_of_pieces, counter_pieces, row, column + 1);
			else:
				move_pieces_clockwise(number_of_pieces, counter_pieces, row - 1, column);
	else:
		if(row == 0):
			if(column == 0):
				if(board[row][column + 1] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row][column + 1] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row + 1, column);
			elif(column == 1):
				if(board[row][column + 1] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row][column + 1] = "X";
					board[row][column] = "";		
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row, column - 1);
			else:
				if(board[row + 1][column] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row + 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row, column - 1);
		elif(row == 1):
			if(column == 0):
				if(board[row - 1][column] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");
				else:			
					board[row - 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row + 1, column);
			else:
				if(board[row + 1][column] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row + 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row - 1, column);
		else:
			if(column == 0):
				if(board[row - 1][column] == "X"):
					print_board();
					print("no moves possible");
					print("\n");
				else:			
					board[row - 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row, column + 1);
			elif(column == 1):
				if(board[row][column - 1] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");		
				else:		
					board[row][column - 1] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row, column + 1);
			else:
				if(board[row][column - 1] == "X"):					
					print_board();
					print("no moves possible");
					print("\n");	 
				else:
					board[row][column - 1] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_clockwise(number_of_pieces, counter_pieces + 1, row - 1, column);
				
def move_pieces_counterclockwise(number_of_pieces, counter_pieces, row, column):
	if (counter_pieces == number_of_pieces):
		return;
	if(board[row][column] != "X"):
		if(row == 0):
			if(column == 0):
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row, column + 1);
			elif(column == 1):		
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row, column + 1);
			else:
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row + 1, column);
		elif(row == 1):
			if(column == 0):
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row - 1, column);
			else:
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row + 1, column);
		else:
			if(column == 0):
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row - 1, column);
			elif(column == 1):	
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row, column - 1);
			else:
				move_pieces_counterclockwise(number_of_pieces, counter_pieces, row, column - 1);	
	else:
		if(row == 0):
			if(column == 0):				
				if(board[row + 1][column] == "X"):
					print_board();
					print("no moves possible");
					print("\n");					
				else:
					board[row + 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row, column + 1);
			elif(column == 1):				
				if(board[row][column - 1] == "X"):
					print_board();
					print("no moves possible");
					print("\n");					
				else:
					board[row][column - 1] = "X";
					board[row][column] = "";	
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row, column + 1);
			else:				
				if(board[row][column - 1] == "X"):
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row][column - 1] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row + 1, column);
		elif(row == 1):
			if(column == 0):			
				if(board[row + 1][column] == "X"):
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row + 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row - 1, column);
			else:				
				if(board[row - 1][column] == "X"):
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row - 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row + 1, column);
		else:
			if(column == 0):				
				if(board[row][column + 1] == "X"):
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row][column + 1] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row - 1, column);
			elif(column == 1):				
				if(board[row][column + 1] == "X"):
					print_board();
					print("no moves possible");
					print("\n");		
				else:
					board[row][column + 1] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row, column - 1);
			else:				
				if(board[row - 1][column] == "X"):
					print_board();
					print("no moves possible");
					print("\n");
				else:
					board[row - 1][column] = "X";
					board[row][column] = "";
					print_board();
					print("\n");
				move_pieces_counterclockwise(number_of_pieces, counter_pieces + 1, row, column - 1);	
start()