The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. I have solved this program earlier, but am trying to rework my code to mirror that which I used to fashion a sudoku solver. I cannot seem to find the logical error but when I run the code, nothing prints. My program is attached below and if anyone could find my error that would be very helpful!
import numpy as np
def main():
global n
n = input("Enter N")
n = int(n)
global board
board = np.zeros((n,n), dtype=int)
solve_board()
def solve_board():
for row in range(n):
for col in range(n):
if board[row][col] == 0: #no queen
if (is_valid (board,row,col,n)):
board[row][col] = 1 #Assigning 1 for queen
solve_board()
board[row][col] = 0
return False
print('-'*n)
for row in board:
for col in row:
if col == 1:
print ("Q", end = " ")
else:
print (".", end = " ")
def is_valid(board,i,j,n):
if 1 in board[i]: #Checking row
return False
for row in range(0,i): #Checking column
if (board[row][j]==1):
return False
x,y = i,j
while (x>=0 and y>=0): #left diagonal
if (board[x][y]==1):
return False
x-=1
y-=1
x,y = i,j
while (x>=0 and y<n): #right diagonal
if (board[x][y]==1):
return False
x-=1
y+=1
return True
if __name__ == "__main__":
main()
This is how I had solved this code earlier, with solve_board being altered as followed.
def solve_board(row):
if(row == n):
print('-'*n)
for row in board:
for col in row:
if col == 1:
print ("Q", end = " ")
else:
print (".", end = " ")
print("")
else:
for col in range(n):
if (is_valid (board,row,col,n)):
board[row][col]=1
solve_board(row+1)
board[row][col] = 0
return False
Here is where the inspiration for my current code came from, a sudoku solver that I designed where I used 2 nested for loops; one for rows and one for columns. Based on this I had altered my solve_board(row) in my original n-queens code to the current function without a parameter. This sudoku code works perfectly.
def solve_board():
global board
for rowno in range(9):
#print ("row",rowno)
for colno in range(9):
#print("col",colno)
if board[rowno][colno] == 0:
for i in range(1,10):
#print(i)
if (is_valid(board,rowno,colno,i)):
board[rowno][colno]=i
solve_board()
board[rowno][colno]=0
return False
print (np.matrix(board))
I think the issue might lie in the fact that in the N-Queens problem the board does not fill up, i.e there are still 0s while for sudoku the entire board fills up and therefore when the ''if board[row][col] == 0'' is proven false exits the loop and prints. In the N-Queens problem since zero's are always present, it becomes an issue.
Try this
import numpy as np
def main():
global n
n = input("Enter N: ")
n = int(n)
global board
board = np.zeros((n,n), dtype=int)
solve_board()
def print_board():
print('-'*n)
for row in board:
for col in row:
if col == 1:
print ("Q", end = " ")
else:
print (".", end = " ")
print()
def is_valid(board,i,j,n):
if 1 in board[i]: #Checking row
return False
for row in range(0, n): #Checking column
if (board[row][j]==1):
return False
for k in range(0,n):
for l in range(0,n):
if (k+l==i+j) or (k-l==i-j):
if board[k][l]==1:
return False
return True
def solve_board():
for row in range(n):
for col in range(n):
if board[row][col] == 0: #no queen
if (is_valid (board,row,col,n)):
board[row][col] = 1 #Assigning 1 for queen
if np.count_nonzero(board) == n:
print_board()
return True
solve_board()
board[row][col] = 0
else:
return False
if __name__ == "__main__":
main()
Related
I recently tried to code an AI to solve the connect-four game. I've come quite far but I am now stuck with a minor mistake in the code that I just cant locate. Generally the algorithm played great but sometimes the algorithms ignores a row of 3 pieces the opponent has which results in the loss of the algorithm. As you will see, I have constructed the evaluation function so that positions like that should be rated with a extremely low score, which it does. Also the score of a position where the algorithm has lost is always rated with -inf. Therefore I can't imagine why the algorithm would be unable to counter positions like that.
The bot relies on a framework which I can't upload here thus I am sorry that the code itself without changes cant be simply executed.
from aiagent import AiAgent
import math
import copy
import numpy as np
class MinMaxAgent(AiAgent):
def __init__(self):
'''
Creates the board.
'''
self.board = [[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0],
[0,0,0,0,0,0]]
def getNextMove(self):
'''
Calculate the index of the player move and store it on the board. Return that value.
'''
self._getMinMax(board=self.board)
self.board[self.bestIndexCol][self._getRowIndex(self.bestIndexCol, self.board)] = 1
print(f'Eval: {self._evaluate(self.board)}')
return (self.bestIndexCol, self._getRowIndex(self.bestIndexCol, self.board))
def handleNextMove(self, indexCol):
'''
Store the index of the enemy move in the board.
'''
self.board[indexCol[0]][self._getRowIndex(indexCol[0], self.board)] = -1
print(f'Eval: {self._evaluate(self.board)}')
def _getRowIndex(self, indexCol, board):
'''
Get the index of the row of a column within a board.
'''
for indexRow, elementRow in enumerate(board[indexCol]):
if elementRow == 0:
return indexRow
def _getValidIndex(self, board):
'''
Get all the valid indices of a board.
'''
validMoves = []
for indexCol, col in enumerate(board):
if col.count(0) != 0:
validMoves.append(indexCol)
return validMoves
def _getMinMax(self, board, depth=6, player=1, alpha=-math.inf, beta=math.inf):
'''
Calculates the best move within a specific depth.
'''
if depth == 0:
return self._evaluate(board)
elif self._isTerminalState(board) == 0:
return self._evaluate(board)
elif self._isTerminalState(board) == -1:
return -math.inf
elif self._isTerminalState(board) == 1:
return math.inf
if player == 1:
resultMax = -math.inf
self.bestIndexCol = None
for indexCol in self._getValidIndex(board):
# Mutate the board
self.nextBoard = copy.deepcopy(board)
self.nextBoard[indexCol][self._getRowIndex(indexCol, board)] = 1
# Calls itself with a by one decremented depth and the change of player
self.resultMinMax = self._getMinMax(board=self.nextBoard, depth=depth-1, player=-1, alpha=alpha, beta=beta)
# Take the board state with the most amount of points
if self.resultMinMax > resultMax:
resultMax = self.resultMinMax
self.bestIndexCol = indexCol
# Change alpha if the boardstate is evaluated with more points
if self.resultMinMax > alpha:
alpha = self.resultMinMax
# Break the loop if on a alphaboundry
if alpha >= beta:
break
return resultMax
elif player == -1:
resultMin = math.inf
for indexCol in self._getValidIndex(board):
# Mutate the board
self.nextBoard = copy.deepcopy(board)
self.nextBoard[indexCol][self._getRowIndex(indexCol, board)] = -1
# Calls itself with a by one decremented depth and the change of player
self.resultMinMax = self._getMinMax(board=self.nextBoard, depth=depth-1, player=1, alpha=alpha, beta=beta)
# Take the board state with the least amount of points
if self.resultMinMax < resultMin:
resultMin = self.resultMinMax
# Change beta if the boardstate is evaluated with less points
if self.resultMinMax < beta:
beta = self.resultMinMax
# Break the loop if on a betaboundry
if alpha >= beta:
break
return resultMin
def _isTerminalState(self, board):
'''
Checks the board for a terminal state of the board:
Return: 0 for a draw;
1 for a victory;
-1 for a defeat;
'''
# Evaluate draw
if [board[col].count(0) for col in range(7)] == [0,0,0,0,0,0,0]:
return 0
# Evaluate vertical for terminal state
for col in range(7): # columns
for row in range(3): # rows
if [board[col][row + i] for i in range(4)] == [1,1,1,1]:
return 1
elif [board[col][row + i] for i in range(4)] == [-1,-1,-1,-1]:
return -1
# Evaluate horizontal for terminal state
for col in range(4): # columns
for row in range(6): # rows
if [board[col + i][row] for i in range(4)] == [1,1,1,1]:
return 1
elif [board[col + i][row] for i in range(4)] == [-1,-1,-1,-1]:
return -1
# Evaluate diagonal for terminal state
for col in range(4): # columns
for row in range(3): # rows
if [board[col + i][row + i] for i in range(4)] == [1,1,1,1]:
return 1
elif [board[col + i][row + i] for i in range(4)] == [-1,-1,-1,-1]:
return -1
for col in range(4): # columns
for row in range(3, 6): # rows
if [board[col + i][row - i] for i in range(4)] == [1,1,1,1]:
return 1
elif [board[col + i][row - i] for i in range(4)] == [-1,-1,-1,-1]:
return -1
def _evaluateSection(self, section):
'''
Evaluates every section of the board and adds points according to the amount of elements of the same actor in a section:
PLAYER: 4-in-a-row: +inf
ENEMY: 4-in-a-row: -inf
PLAYER: 3-in-a-row: +1000
ENEMY: 3-in-a-row: -3000
PLAYER: 2-in-a-row: +200
ENEMY: 2-in-a-row: -600
'''
self.section_evaluation = 0
if section.count(1) == 4:
self.section_evaluation += math.inf
elif section.count(-1) == 4:
self.section_evaluation -= math.inf
elif section.count(1) == 3 and section.count(0) == 1:
self.section_evaluation += 1000
elif section.count(-1) == 3 and section.count(0) == 1:
self.section_evaluation -= 3000
elif section.count(1) == 2 and section.count(0) == 2:
self.section_evaluation += 200
elif section.count(-1) == 2 and section.count(0) == 2:
self.section_evaluation -= 600
return self.section_evaluation
def _evaluate(self, board):
'''
Takes sections of the board to evaluate.
'''
self.evaluation = 0
# Evaluate vertical sections
for col in range(7): # columns
for row in range(3): # rows
self.section = [board[col][row + i] for i in range(4)]
self.evaluation += self._evaluateSection(self.section)
# Evaluate horizontal sections
for col in range(4): # columns
for row in range(6): # rows
self.section = [board[col + i][row] for i in range(4)]
self.evaluation += self._evaluateSection(self.section)
# Evaluate diagonal sections
for col in range(4): # columns
for row in range(3): # rows
self.section = [board[col + i][row + i] for i in range(4)]
self.evaluation += self._evaluateSection(self.section)
for col in range(4): # columns
for row in range(3, 6): # rows
self.section = [board[col + i][row - i] for i in range(4)]
self.evaluation += self._evaluateSection(self.section)
return self.evaluation
I already redesigned the evaluation function and checked the minmax-algorithm which should include all the possible sources of this error but I wasn't able to find any satisfying answer.
My problem: Python tab closes on "3 1" input.
This code is heavily inspired by Hafeezul Kareem Shaik's tic-tac-toe game on geekflare.
My code:
import random
class TicTacToe:
def __init__(self):
self.board = []
def create_board(self):
for r0w in range(3):
row = []
for c0l in range(3):
row.append('-')
self.board.append(row)
def get_random_first_player(self):
return random.randint(0,1)
def fix_spot(self, row, col, player):
self.board[row][col] = player
def is_player_win(self, player):
win = None
n = len(self.board)
#checking rows
for r0w in range(n):
win = True
for c0l in range(n):
if self.board[r0w][c0l] != player:
win = False
break
if win:
return win
#checking columns
for r0w in range(n):
win = True
for c0l in range(n):
if self.board[c0l][r0w] != player:
win = False
break
if win:
return win
#checking diagonals
win = True
for i in range (n):
if self.board[i][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if self.board[r0w][n - 1 - r0w] != player:
win = False
break
if win:
return win
return False
for row in self.board:
for item in row:
if item == '-':
return False
return True
def is_board_filled(self):
for row in self.board:
for item in row:
if item == "-":
return False
return True
def swap_player_turn(self, player):
return 'X' if player =='O' else 'O'
def show_board(self):
for row in self.board:
for item in row:
print(item, end=" ")
print()
def start(self):
self.create_board()
player = 'X' if self.get_random_first_player() == 1 else 'O'
while True:
print(f"Player {player} turn")
self.show_board()
# user input
row, col = list(
map(int, input("Enter row and column numbers to fix spot: ").split()))
print()
#fixing spot
self.fix_spot(row - 1, col - 1, player)
#has current player won
if self.is_player_win(player):
print(f"{player} Wins!")
break
#is game a draw
if self.is_board_filled():
print("Draw!")
break
#swapping turn
player = self.swap_player_turn(player)
#final board
print()
self.show_board()
tic_tac_toe = TicTacToe()
tic_tac_toe.start()
The Tic_Tac-Toe Game worked up until I typed "3 1". I tested multiple games and it would always close after that input.
I have tried editing the scope of the following, as I believe that is what is causing it to malfunction
if win:
return win
return False
Unfortunately that has not fixed the problem. Any ideas or suggestions?
This code is so badly written that I wouldn't take it as an example for anything.
But if you really want to know why it ends on 3 1 as input, you should look here:
win = True
for i in range(n):
if self.board[r0w][n - 1 - r0w] != player:
win = False
break
if win:
return win
return False
Here r0w is not reset, it has the last value from the for loop, which is 2, and it's not changing in the loop, so there's only one check made, which is 3 1, translated to 2 0, which is the current player, so that's an instant win.
I assume this was supposed to be a loop to check the other diagonal, but it's clearly flawed.
I created a sudoku solver with backtracking algorithm (Python 3.8). It is a recursive algorithm. The sudoku board (puzzle) is a global variable (multiple functions need to share it). The solve() function does its task, but the value of the board doesn't change even after using the global keyword. Your help is needed.
The code:
board = [
[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
def isPossible(y, x, val): # checks if it is legal to put a value at a certain position
for row in board: # row condition
if val == row[x]:
return False
for col in board[y]: # column condition
if val == col:
return False
# subcell condition
subCellRow = (y // 3) * 3
subCellCol = (x // 3) * 3
for row in board[subCellRow:subCellRow + 3]:
for col in row[subCellCol:subCellCol + 3]:
if val == col:
return False
return True
def solve():
global board
for y in range(9):
for x in range(9):
if board[y][x] == 0:
for n in range(1, 10):
if isPossible(y, x, n): # python stairs
board[y][x] = n
solve() # recursion starts
board[y][x] = 0 # 1-line backtracking algorithm
return
printPuzzle() # prints the solved puzzle
return True
def printPuzzle(): # to display the puzzle
print()
for row in board:
for val in row:
print(val, end = ' ')
print()
printPuzzle()
solve()
printPuzzle() # prints the unsolved board
This wasn't the mistake of the global variable, this is happening because there is a slight mistake in your implementation of the algorithm which is reassigning the value to zero at board[y][x] = 0.
Here is the implementation which I think is correct.
board = [
[5,3,0,0,7,0,0,0,0],
[6,0,0,1,9,5,0,0,0],
[0,9,8,0,0,0,0,6,0],
[8,0,0,0,6,0,0,0,3],
[4,0,0,8,0,3,0,0,1],
[7,0,0,0,2,0,0,0,6],
[0,6,0,0,0,0,2,8,0],
[0,0,0,4,1,9,0,0,5],
[0,0,0,0,8,0,0,7,9]]
def isPossible(y, x, val): # checks if it is legal to put a value at a certain position
for row in board: # row condition
if val == row[x]:
return False
for col in board[y]: # column condition
if val == col:
return False
# subcell condition
subCellRow = (y // 3) * 3
subCellCol = (x // 3) * 3
for row in board[subCellRow:subCellRow + 3]:
for col in row[subCellCol:subCellCol + 3]:
if val == col:
return False
return True
def solve():
global board
for y in range(9):
for x in range(9):
if board[y][x] == 0:
for n in range(1, 10):
if isPossible(y, x, n): # python stairs
board[y][x] = n
if solve():
return True # recursion starts
board[y][x] = 0
return False
printPuzzle() # prints the solved puzzle
return True
def printPuzzle(): # to display the puzzle
print()
for row in board:
for val in row:
print(val, end = ' ')
print()
printPuzzle()
solve()
printPuzzle() # prints the unsolved board
Output I am getting:
I'm working on a connect4 and I have an issue on the piece slide down part.
When I launch the game, the pieces stacked themself only once like that.
000000
000000
000000
000000
000000
PP0000
XX0000
But after that, it doesn't stack anymore. If the user chose to put a piece on the first or second row it won't stack the piece over the actual one. I tried to figure out where the issue was coming from but I don't see it and beeing new in python doesn't help a lot.
board = []
for x in range(0, 7):
board.append(["O"] * 6)
def print_board(board):
for row in board:
print(" ".join(row))
def user_one(board):
status = 0
clm = 0
first_x = int(input("Enter a Row: "))
for i in board:
while i == "0":
clm += 1
if board[clm - 1][first_x - 1] == "P":
board[clm - 2][first_x - 1] = "X"
else:
board[clm - 1][first_x - 1] = "X"
status = 1
print_board(board)
def user_two(board):
status = 0
clm = 0
second_p = int(input("Enter a Row: "))
for i in board:
while i == "0":
clm += 1
if board[clm - 1][second_p - 1] == "X":
board[clm - 2][second_p - 1] = "P"
else:
board[clm - 1][second_p - 1] = "P"
status = 2
print_board(board)
def launch_game(board):
while True:
user_one(board)
user_two(board)
launch_game(board)
I think overall was a bit of confusion between column and row. I took your code and modified it a little bit. So this should work:
board = []
for x in range(0, 7):
board.append(["O"] * 6)
def print_board(board):
for row in board:
print(" ".join(row))
def user_one(board):
clm = 0
selected_column = int(input("Enter a column: "))
for i in reversed(range(len(board))):
if board[i][selected_column-1] == "O":
board[i][selected_column-1] = "X"
break
print_board(board)
def user_two(board):
clm = 0
selected_column = int(input("Enter a column: "))
for i in reversed(range(len(board))):
if board[i][selected_column-1] == "O":
board[i][selected_column-1] = "P"
break
print_board(board)
def launch_game(board):
while True:
user_one(board)
user_two(board)
launch_game(board)
You can still improve on removing unused variables, names and refactoring the functions (so that you only have one for your piece slide down).
I have been trying to program a variant of connect four for my programming class. The board is 6x8 in size. In the variant I'm trying to program, the winning condition is to essentially build an L.
This means any construction of the form
X
X
X X
is a winning condition.
I have been trying to make a function that checks every single column for the same symbol consecutively to build a pair. And a function to do the same for every row. With these two functions I would then check if 2 pairs are consecutive, because no matter how you combine a vertical and horizontal pair, it will always build an 'L'.
To make a clear board I'm using
def ClearBoardSingle():
global Board
Board = [['0' for i in range(8)] for i in range(6)]
BoardPrint()
PlayerMoveSingle()
And for my interface I'm using
def BoardPrint():
global Board
global GameMoves
global PlayerTurn
global Player1Symbol
global Player2Symbol
print('\n\nMoves done: ' + str(GameMoves))
print('To Restart: R | To Quit: Q')
print('Valid choices: 1, 2, 3, 4, 5, 6, 7, 8')
if PlayerTurn == 0:
print('It\'s ' +str(Player1Symbol) + '\'s Turn')
if PlayerTurn == 1:
print('It\'s ' +str(Player2Symbol) + '\'s Turn')
print(Board[0])
print(Board[1])
print(Board[2])
print(Board[3])
print(Board[4])
print(Board[5])
I already figured out how to change Variables inside the Board, and I'm pretty much done. The only thing I don't know how to implement is the winning condition. I tried this function for the Rows:
def VerticalList(Column):
global Board
global Choice
global Row0
Column = int(Column)
Choice = int(Choice)
print(Column,' C')
while Column > 0:
for Board[Column][Choice] in range(Column):
Row0.append(Board[Column][Choice])
if Column ==6 or Column == -1:
break
else:
VerticalList(Column-1)
if Column ==0:
break
else:
continue
if Column == 0:
Column += 1
while Column < 5:
Column +=1
if Row0[Column] == Row0[Column-1]:
print('Pair')
else:
print('No Pair')
pass
else:
pass
But it enters an endless Loop.
I have no ideas anymore on how to implement the winning condition. I'd appreciate any kind of help or ideas. If you want me to post the whole code or other kinds of snippets, ask for them.
Thank you in anticipation!
Cool problem, below looks like a lot of code, but it's not really. I haven't checked this extensively, so I'm not confident that it doesn't find false positives, but it seems to find L's that it should be finding. The main thing I did was use itertools.combinations to take all 4-sized groups of the positions of X's and then check if they looked like patterns I was expecting for L's. In check_four_group I look at the differences within the row and columns.
from itertools import combinations
def disp_board(board):
for row in board:
print(row)
def check_winning(board):
winning = False
#Find all row,col positions of the X's
x_poses = [(i,j) for i in range(6) for j in range(8) if board[i][j] == 'X']
#Loop through every combination of four X's since it takes four to make the 'L'
for group in combinations(x_poses,4):
if(check_four_group(group)):
winning = True
break
return winning
def check_four_group(group):
rows,cols = zip(*[(r,c) for r,c in group])
row_diffs = [rows[i+1]-rows[i] for i in range(len(rows)-1)]
col_diffs = [cols[i+1]-cols[i] for i in range(len(cols)-1)]
#Uncomment this to print the row and col diffs
#print(row_diffs)
#print(col_diffs)
# Finds:
# X
# X
# X X
if row_diffs == [1,1,0] and col_diffs == [0,0,1]:
return True
# Finds:
# X
# X
# X X
elif row_diffs == [1,1,0] and col_diffs == [0,-1,1]:
return True
# Finds:
# X X
# X
# X
elif row_diffs == [0,1,1] and col_diffs == [1,0,0]:
return True
# Finds:
# X X
# X
# X
elif row_diffs == [0,1,1] and col_diffs == [1,-1,0]:
return True
# Otherwise it's not there at all (not thinking about horizontal L's but could add that)
else:
return False
#Test case 1
def test_case_1():
board = [['0' for i in range(8)] for i in range(6)]
board[2][1] = 'X'
board[2][2] = 'X'
board[3][1] = 'X'
board[4][1] = 'X'
return board
#Test case 2
def test_case_2():
board = [['0' for i in range(8)] for i in range(6)]
board[2][1] = 'X'
board[2][0] = 'X'
board[3][1] = 'X'
board[4][1] = 'X'
return board
#Test case 3
def test_case_3():
board = [['0' for i in range(8)] for i in range(6)]
board[1][0] = 'X'
board[2][0] = 'X'
board[3][0] = 'X'
board[3][1] = 'X'
return board
#Test case 4
def test_case_4():
board = [['0' for i in range(8)] for i in range(6)]
board[1][2] = 'X'
board[2][2] = 'X'
board[3][2] = 'X'
board[3][1] = 'X'
return board
##################
#Start of program#
##################
board = test_case_1()
#board = test_case_2()
#board = test_case_3()
#board = test_case_4()
disp_board(board)
if check_winning(board):
print('Victory')
else:
print('Keep playing')