P2 init des Schachbretts und Visualisierung in der Ausgabe
This commit is contained in:
112
JPNB/AI_2.ipynb
Normal file
112
JPNB/AI_2.ipynb
Normal file
@@ -0,0 +1,112 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"metadata": {},
|
||||
"cell_type": "markdown",
|
||||
"source": [
|
||||
"# 8 Queen Problem\n",
|
||||
"## Install Requirements\n",
|
||||
"create a virtual environment in current directory by\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"python3 -m venv .env # macos\n",
|
||||
"python -m venv .env # linux\n",
|
||||
"```\n",
|
||||
"## Imports"
|
||||
],
|
||||
"id": "3cf53c1e"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "2da4ea5d",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-06-17T16:03:48.531191Z",
|
||||
"start_time": "2025-06-17T16:03:48.529197Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"import random"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 1
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "712bccdf",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-06-17T16:03:48.587628Z",
|
||||
"start_time": "2025-06-17T16:03:48.585433Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"class Field:\n",
|
||||
" def __init__(self, init_state = []):\n",
|
||||
" self.state = []\n",
|
||||
"\n",
|
||||
" if init_state is []:\n",
|
||||
" for i in range(8):\n",
|
||||
" self.state.append(random.randint(1,8))\n",
|
||||
" print(i)\n",
|
||||
" else:\n",
|
||||
" self.state = init_state.copy()\n",
|
||||
" \n",
|
||||
" def print_field(self):\n",
|
||||
" for row in range(8):\n",
|
||||
" print(row)\n",
|
||||
" for line in range(8):\n",
|
||||
" if row+1 is self.state[line]:\n",
|
||||
" print(\"Q\")\n",
|
||||
" else:\n",
|
||||
" print(\" \")\n",
|
||||
" print(\"\\n\")\n",
|
||||
" \n",
|
||||
" def get_state(self):\n",
|
||||
" return self.state"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 2
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"id": "25dc1068",
|
||||
"metadata": {
|
||||
"ExecuteTime": {
|
||||
"end_time": "2025-06-17T16:03:48.595773Z",
|
||||
"start_time": "2025-06-17T16:03:48.593755Z"
|
||||
}
|
||||
},
|
||||
"source": [
|
||||
"def main():\n",
|
||||
" new_field = Field()\n",
|
||||
" new_field.print_field()\n",
|
||||
"\n",
|
||||
" print(\"Hello\")"
|
||||
],
|
||||
"outputs": [],
|
||||
"execution_count": 3
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": ".env",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -386,7 +386,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.13.3"
|
||||
"version": "3.13.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
44
P2.py
Normal file
44
P2.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import random
|
||||
|
||||
class Field:
|
||||
def __init__(self, init_state=None):
|
||||
self.state = []
|
||||
|
||||
if init_state is None:
|
||||
for i in range(8):
|
||||
self.state.append(random.randint(1,8)) # row number (0:8] => [1:8]
|
||||
else:
|
||||
self.state = init_state.copy()
|
||||
|
||||
def print_field(self):
|
||||
print(" ┌───┬───┬───┬───┬───┬───┬───┬───┐")
|
||||
for row in range(8,0,-1): # (0:8]
|
||||
row_string = ""
|
||||
for line in range(8):
|
||||
if row is self.state[line]: # is there a Queen in this line (spalte) in this row
|
||||
row_string += "Q │ "
|
||||
else:
|
||||
row_string += " │ "
|
||||
print(f"{row} | {row_string}")
|
||||
if row > 1 : print(" ├───┼───┼───┼───┼───┼───┼───┼───┤")
|
||||
|
||||
print(" └───┴───┴───┴───┴───┴───┴───┴───┘")
|
||||
print(" A B C D E F G H ")
|
||||
|
||||
print(self.state)
|
||||
|
||||
def get_state(self):
|
||||
return self.state
|
||||
|
||||
def collisions(self, current_state):
|
||||
# wagerechte haben die gleiche zahl stehe
|
||||
# diagonale haben einen wert der um den abstand gemindert ist => gleichseitiges rechtwinkliges Dreieck
|
||||
# Beachte die Spalten/ Linien Nr ist um eins verringert [0, 1, ...,7]
|
||||
return 0
|
||||
|
||||
|
||||
def main():
|
||||
new_field = Field()
|
||||
new_field.print_field()
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user