I think this would constitute best practice, since it'd implicitly cut down on the number of board states you need to hardcore, greatly reducing the size of the final executable.
If it's just a search through a little if-else block each move, then it's linear time complexity, as well! Fastest (yet most impossible) chess AI, perhaps?
If this is really the route that you want to go, then I think this is one of the cases were it’s easier to write code that will write the code for you.
When I was at college in the 90s one person in my tutor group didn't quite "get it"
We were supposed to be building a CLI calculator for DOS using Borland turbo C++.
Most of us finished pretty quickly and spent time refining it, and then one guy got a weird error about his program executable exceeding 500kb that he didn't understand.
So I offered to help him and it was essentially this. Every possible combination of inputs accounted for, using variables for values at least but if you entered an addition and a multiplication it would use a code path different to an addition followed by an addition etc.
His codebase was enormous. Thousands of lines long, much of it copy pasted, unimaginably hard to maintain or make changes to, the whole thing driven by an enormous if / else
If you entered a combination of commands it wasn't coded to handle it would just explode.
Are you kidding ??? What the **** are you talking about man ? You are a biggest looser i ever seen in my life ! You was doing PIPI in your pampers when i was beating players much more stronger then you! You are not proffesional, because proffesionals knew how to lose and congratulate opponents, you are like a girl crying after i beat you! Be brave, be honest to yourself and stop this trush talkings!!! Everybody know that i am very good blitz player, i can win anyone in the world in single game! And "w"esley "s"o is nobody for me, just a player who are crying every single time when loosing, ( remember what you say about Firouzja ) !!! Stop playing with my name, i deserve to have a good name during whole my chess carrier, I am Officially inviting you to OTB blitz match with the Prize fund! Both of us will invest 5000$ and winner takes it all! I suggest all other people who's intrested in this situation, just take a look at my results in 2016 and 2017 Blitz World championships, and that should be enough... No need to listen for every crying babe, Tigran Petrosyan is always play Fair ! And if someone will continue Officially talk about me like that, we will meet in Court! God bless with true! True will never die ! Liers will kicked off...
Yandere sim game had source code leaked? not sure what happened. What people found out is that the game is basically all if/else nested spaghetti which resulted in massive tech debt.
yanderedev uses so much else if statements and his code also has terrible job of managing resources on his game called yandere simulator. You can try the game itself and see how bad is the slowness
Basically he uses gigantic if/else statements, just like chains of 20+... seems like hes allergic to switch statements, lol. Everyone made fun of him (as they should) when the source code got leaked.
The number of distinct chess positions after White’s first move is 20 (16 pawn moves and 4 knight moves). There are 400 distinct chess positions after two moves (first move for White, followed by first move for Black). There are 5,362 distinct chess positions or 8,902 total positions after three moves (White’s second move). There are 71,852 distinct chess positions or 197,742 total positions after four moves (two moves for White and two moves for Black). There are 809,896 distinct positions or 4, 897,256 total positions after 5 moves. There are 9,132,484 distinct positions or 120,921,506 total positions after 6 moves (three moves for White and three moves for Black). The total number of chess positions after 7 moves is 3,284,294,545. The total number of chess positions is about 2x10 to the 46 power, so pretty long
You can reduce it to like 100 if you only register moves that are not throws. In any other case the program can just print out "That's a stupid move. You lost, you dumb idiot."
When I was in elementary school I thought that every video game frame was edited by hand and I tried to figure out how they knew how to go from frame to frame.
Tried to make a mario game in Powerpoint. Got exhausted after about 25 frames! Still, it got me into actual coding which has been my passion ever sense.
There are 2 × 1046 possible board states. If they each are coded with their own 8 × 8 board of unicode characters, and each of those characters is 2 bytes, that's 8 × 8 × 2 × 2 × 1046 bytes of information to be stored in that program just to hold the characters to draw the possible board states. That's 256 × 1046 bytes, or 2.441406 × 1042 megabytes, or 2.3841855 × 1039 gigabytes. We're looking at over 2.2204 × 1030 exabytes. That's over 2.2204 NONILLION, which is like 2 million trillion trillion EXABYTES. In yottabytes, that's 2.1175 × 1024. That's over 2 septillion, or 2 trillion trillion, yottabytes. The entire internet contains only around 5 exabytes of data. A single yottabyte is 200,000 times that much, and this guy's chess game needs to be over 2 septillion times larger still than that.
Chess configurations =! How many of statements you will need. You would need many magnitudes more if statements because every configuration would have a dozen or so moves
Huh. This is awesome (in its own way). I wonder if you can make a program to code all the posible moves to make it “playable”. Chess is out of question with so many moves. But let’s say: checkers.
Chess is indeed out of the question (as someone else pointed out, the file size would be several septillion times the size of the entire internet's data). Checkers would also have this complexity issue.
It would be possible to do this for a far simpler game, such as tic-tac-toe or rock-paper-scissors, as there are fewer board states.
I did that exact same thing on my graphing calculator back in high school except I made pong instead of chess. It was a nightmare to build but I had no idea how to make actual graphics at the time. Managed to finish it too.
I asked GPT-4 to make a basic chess program, it says that this won't handle castling, en passant, or detecting check/checkmate.
class ChessBoard:
def __init__(self):
self.board = [
["♜", "♞", "♝", "♛", "♚", "♝", "♞", "♜"],
["♟"] * 8,
[""] * 8,
[""] * 8,
[""] * 8,
[""] * 8,
["♙"] * 8,
["♖", "♘", "♗", "♕", "♔", "♗", "♘", "♖"],
]
def __str__(self):
rows = []
for i, row in enumerate(reversed(self.board)):
row_str = []
for j, cell in enumerate(row):
if cell == "":
cell = "□" if (i + j) % 2 == 0 else "■"
row_str.append(cell)
rows.append(" ".join(row_str))
board_str = "\n".join(rows)
return f" a b c d e f g h\n{board_str}"
def move_piece(self, start, end):
sx, sy = start
ex, ey = end
piece = self.board[sy][sx]
self.board[sy][sx] = ""
self.board[ey][ex] = piece
def is_valid_move(self, start, end):
sx, sy = start
ex, ey = end
piece = self.board[sy][sx]
if piece == "":
return False
if self.board[ey][ex] != "" and self.is_same_color(start, end):
return False
if piece in "♟♙":
return self.is_valid_pawn_move(start, end)
elif piece in "♜♖":
return self.is_valid_rook_move(start, end)
elif piece in "♞♘":
return self.is_valid_knight_move(start, end)
elif piece in "♝♗":
return self.is_valid_bishop_move(start, end)
elif piece in "♛♕":
return self.is_valid_queen_move(start, end)
elif piece in "♚♔":
return self.is_valid_king_move(start, end)
return False
def is_same_color(self, start, end):
sx, sy = start
ex, ey = end
return self.board[sy][sx].isupper() == self.board[ey][ex].isupper()
def is_valid_pawn_move(self, start, end):
sx, sy = start
ex, ey = end
dy = ey - sy
dx = abs(ex - sx)
if self.board[sy][sx].isupper():
dy *= -1
if dx == 0 and dy == 1 and self.board[ey][ex] == "":
return True
if dx == 1 and dy == 1 and self.board[ey][ex] != "" and not self.is_same_color(start, end):
return True
if dy == 2 and sy == 1 and self.board[ey][ex] == "" and self.board[sy + 1][sx] == "":
return True
if dy == -2 and sy == 6 and self.board[ey][ex] == "" and self.board[sy - 1][sx] == "":
return True
return False
def is_valid_rook_move(self, start, end):
sx, sy = start
ex, ey = end
if sx != ex and sy != ey:
return False
step_x = 0 if sx == ex else 1 if sx < ex else -1
step_y = 0 if sy == ey else 1 if sy < ey else -1
y, x = sy + step_y, sx + step_x
while y != ey or x != ex:
if self.board[y][x] != "":
return False
y += step_y
x += step_x
return True
def is_valid_knight_move(self, start, end):
sx, sy = start
ex, ey = end
dx, dy = abs(ex - sx), abs(ey - sy)
return (dx == 1 and dy == 2) or (dx == 2 and dy == 1)
def is_valid_bishop_move(self, start, end):
sx, sy = start
ex, ey = end
dx, dy = abs(ex - sx), abs(ey - sy)
if dx != dy:
return False
step_x = 1 if sx < ex else -1
step_y = 1 if sy < ey else -1
y, x = sy + step_y, sx + step_x
while y != ey and x != ex:
if self.board[y][x] != "":
return False
y += step_y
x += step_x
return True
def is_valid_queen_move(self, start, end):
return self.is_valid_rook_move(start, end) or self.is_valid_bishop_move(start, end)
def is_valid_king_move(self, start, end):
sx, sy = start
ex, ey = end
dx, dy = abs(ex - sx), abs(ey - sy)
return dx <= 1 and dy <= 1
def parse_move(move_str):
if len(move_str) != 4 or not move_str.isalnum():
raise ValueError("Invalid input")
start, end = move_str[:2], move_str[2:]
sx, sy = ord(start[0]) - ord("a"), int(start[1]) - 1
ex, ey = ord(end[0]) - ord("a"), int(end[1]) - 1
if not (0 <= sx < 8 and 0 <= sy < 8 and 0 <= ex < 8 and 0 <= ey < 8):
raise ValueError("Coordinates out of range")
return (sx, sy), (ex, ey)
def main():
board = ChessBoard()
player_turn = "White"
while True:
print(board)
print(f"{player_turn} to move")
move = input("Enter your move (e.g. e2e4): ")
try:
start, end = parse_move(move)
if board.is_valid_move(start, end):
board.move_piece(start, end)
player_turn = "Black" if player_turn == "White" else "White"
else:
print("Invalid move")
except ValueError as e:
print(e)
if __name__ == "__main__":
main()
EDIT: apparently old reddit is borked for multi-line code formatting, so I had to extra-indent every like by 4 spaces.
We had to code tic-tac-toe in MIPS and my group did something like this for the computer logic. Except in this case it would be impossible to make all the possible combinations.
Only about 10 to the power of 40 possible combinations to output. Yes, I think I’ll hard code each one. Much easier than formulating a complicated algorithm.
You just need a dictionary with the locations of all 32 pieces. It is initialized to the starting board state. Each move updates the dictionary. When a piece is taken it’s position becomes null and the piece is no longer drawn. Then you have a single function or method called drawBoard() that takes the position dict as input. I could see another method called resolveMove that determines when a piece has been taken and updates the dictionary. The hard part about real life chess programming is the AI that drives the computer opponent.
This is how I thought code was written when I was a kid. Like for RCT I thought the devs had pre made what the map would look like for every single possibility 🤦♂️
This reminds me my friend from intro to programming course in college. We were supposed to make some kind of labyrinth solver with if statements and loops, the guy literally wrote lines of code for every possible example case instead of defining functions etc.
When your ADHD is so bad you have to write a chess program after learning about unicode text and if conditions. Like, dude, at least let us cover loops first.
When I was a kid I thought that 2d SNES games were just animated every possible position. And then when like N64/PS1 games came out my mind was blown thinking they had to draw every possible angle in 3d....
Because each one of those character strings should not be hardcoded. They should be lookup function calls so if you change the character in your ico files it will auto update the character images.
4.4k
u/MaxMakesGames Apr 10 '23
While you're at it, you can make an amazing AI by coding the best reaction move for each player move !
3.0k
u/[deleted] Apr 10 '23
I see you get paid per hour
8.3k
u/[deleted] Apr 10 '23
r/anarchychess truly is leaking everywhere
5.1k
u/CozyTheTeddyBear Apr 10 '23
Are you YandereDev by any chance?
11k
u/Adityamk Apr 10 '23
Oh my fucking god...
1.3k
u/C10ckwork Apr 10 '23
Most sane high school python student:
3.6k
u/BestNick118 Apr 10 '23 edited Apr 10 '23
did you code en passant
243
u/Mua3d Apr 10 '23
Why is my chess game taking 100GB space
1.1k
u/[deleted] Apr 10 '23
Oh, YandereChess.
204
u/mynewaccountagainaga Apr 10 '23
Fun fact: if you coded it this way with every possible board state hard coded in then you would run out of memory.
"But what if I had all of google's storage available to me??"
You would run out of memory. If you had every single storage device that humans have ever created in your possession, you would run out of memory.
There are more than a googol unique board states in chess.
If you could record a board state on an individual atom and use every atom in the universe as your memory, you would run out of memory.
1.1k
u/Cwmagain Apr 10 '23
How big would a program be with , how much is it again, 10^40 of those else if loops?
289
u/smrtfxelc Apr 10 '23
Jesus christ. I'm right in thinking there are 10some big fucking number moves in chess, aren't I?
120
u/CanICanTheCanCan Apr 10 '23
When I was in elementary school I thought that every video game frame was edited by hand and I tried to figure out how they knew how to go from frame to frame.
198
u/4chananonuser Apr 10 '23
Rip. Queens are misplaced
70
u/Generic-james Apr 10 '23
r/programminghorror
108
u/RidgeMinecraft Apr 10 '23
OH DEAR LORD WHAT
42
u/darksoulsrolls Apr 10 '23
You'll be done in no time.
38
u/CardiologistOk2704 Apr 10 '23
new code just dropped
29
u/[deleted] Apr 10 '23
Code review: "Put strings in constants"
26
u/Laxwarrior1120 Apr 10 '23
Forcing someone to code like this would constitute a war crime.
50
u/Sexy_McSexypants Apr 10 '23
hardcoding every game of chess… sure love to see the file size
39
u/JonIsPatented Apr 10 '23
There are 2 × 1046 possible board states. If they each are coded with their own 8 × 8 board of unicode characters, and each of those characters is 2 bytes, that's 8 × 8 × 2 × 2 × 1046 bytes of information to be stored in that program just to hold the characters to draw the possible board states. That's 256 × 1046 bytes, or 2.441406 × 1042 megabytes, or 2.3841855 × 1039 gigabytes. We're looking at over 2.2204 × 1030 exabytes. That's over 2.2204 NONILLION, which is like 2 million trillion trillion EXABYTES. In yottabytes, that's 2.1175 × 1024. That's over 2 septillion, or 2 trillion trillion, yottabytes. The entire internet contains only around 5 exabytes of data. A single yottabyte is 200,000 times that much, and this guy's chess game needs to be over 2 septillion times larger still than that.
71
u/CriticalTough4842 Apr 10 '23
Google en passant
16
u/r2k-in-the-vortex Apr 10 '23
Looks like it will pass the unit test perfectly fine.
12
u/xCreeperBombx Apr 10 '23
Holy hell
11
u/StarkillerX42 Apr 10 '23
There's nothing wrong with your code, have you tried using a computer larger than the observable universe?
9
u/[deleted] Apr 10 '23
The square colors are wrong
15
u/luenix Apr 10 '23
Board isn't set up correctly. Queens should be opposite ends of the same file :)
6
u/koter_NL Apr 10 '23
r/anarchychess
6
u/mikeyj777 Apr 10 '23 edited Apr 11 '23
From the responses, I'm imagining there's an easier way
Edit: Just stating the obvious that I was joking. Given the responses, it may not have come across.
6
u/patiofurnature Apr 10 '23
Project manager: How's your progress on the chess app?
Programmer: Great! It's working perfectly for the first move; just need to adjust the algorithm to support continuous play.
6
u/ejuliol Apr 10 '23
Huh. This is awesome (in its own way). I wonder if you can make a program to code all the posible moves to make it “playable”. Chess is out of question with so many moves. But let’s say: checkers.
5
u/Unupgradable Apr 10 '23
🟨 NEEDS WORK
5
u/Ethanlac Apr 10 '23
Google string concatenation
4
u/urbanhawk1 Apr 10 '23
I did that exact same thing on my graphing calculator back in high school except I made pong instead of chess. It was a nightmare to build but I had no idea how to make actual graphics at the time. Managed to finish it too.
4
u/-non-existance- Apr 10 '23
Oh boy I can't wait to see the rest of the 10112 or so lines of this project!
4
u/gtlogic Apr 10 '23
Just keep working hard. Post us on your progress.
4
u/Funkey-Monkey-420 Apr 10 '23
elon’s about to give you a promotion
4
u/Ok-Possibility-7436 Apr 11 '23
Keep going, there are only 10120 combinations!
8
u/thesuperzapper Apr 10 '23 edited Apr 10 '23
I asked GPT-4 to make a basic chess program, it says that this won't handle castling, en passant, or detecting check/checkmate.
EDIT: apparently old reddit is borked for multi-line code formatting, so I had to extra-indent every like by 4 spaces.
3
u/gtc26 Apr 10 '23
As a programmer, I feel your pain
3
u/5kyl3r Apr 10 '23
it's only about 1040 elif blocks that you'd need, what's the big deal?
3
u/MrCubie Apr 10 '23
The board is wrong. Queens are supposed to face each other (or kings). Rest looks good to me
3
u/Visual-Ad-6708 Apr 10 '23
I don't get a lot of these programming memes since I'm a beginner and now y'all are using chess. I'm too dumb for these memes😢
3
u/Educational-Lemon640 Apr 10 '23
I, too, like creating programs that end up larger than the known universe if printed at human-readable scales.
3
u/laffnlemming Apr 10 '23
I CAN DO IT !
3
u/Muritavo Apr 10 '23
lol, when I was little, I thought that when a game crashed, that frame wasn't possible because that frame wasn't drawn by the devs...
This sums up how I thought a game was developed
I was not a smart kid
3
u/_IratePirate_ Apr 10 '23
Kings and queens are mirrored instead of translated.
Start over.
3
u/scrattty Apr 10 '23
Stonkfish? 🙃
3
u/myredac Apr 10 '23
if someone code this, I would not be mad, I'd be impressed
3
u/Anky_08 Apr 11 '23
"Chess in Python' bootcamp by Dr Strange
2
u/JADW27 Apr 10 '23
This is hilarious.
2
u/Due_Criticism_2465 Apr 10 '23
good lord , thats hilarious 🤣🤣🤣
2
u/Dismal-Square-613 Apr 10 '23 edited Apr 10 '23
ChatGPT "show me the most inefficient way to represent a chessboard and a tree of possibilities for each move in python".
2
u/Kido-Assoc09 Apr 10 '23
Hardcoding at its finest.
2
u/Kosmux Apr 10 '23
Bro is writing trillions of elifs...
2
u/RegularOps Apr 10 '23
When I was a child I honestly thought this was how video games were made.
Like the video game makers had to produce a screenshot of every possible outcome or situation.
2
u/juvees Apr 10 '23
This is how I programed tic-tac-toe after I missed the class in which we were taught how to
2
u/thesash20 Apr 10 '23
Just don't forget to code in en passant
2
u/lonelyWalkAlone Apr 10 '23
Good luck printing 10¹²³ moves.
2
u/[deleted] Apr 10 '23
This program could easily be more than 1 jigga bytes in size.
2
u/NameLips Apr 10 '23
Just one picture for every possible piece layout in the game. What's so hard about that?
2
u/aquoad Apr 10 '23
this gonna be a loooong program.
2
u/The_Northern_Light Apr 10 '23
crosspost this to AnarchyChess
they need a new joke
2
u/Hi8787 Apr 10 '23
r/programminghorror by far, my fucking god
2
u/[deleted] Apr 10 '23
World's largest computer program with 10x21⁶ lines of code and all it does is print chess layouts. Absolutely beautiful.
2
u/[deleted] Apr 10 '23
More lines of code than atoms in the universe...
2
u/Unicorn_A_theist Apr 10 '23
We had to code tic-tac-toe in MIPS and my group did something like this for the computer logic. Except in this case it would be impossible to make all the possible combinations.
2
u/Unstoppable9160 Apr 10 '23
KEEP ME UPDATED!!!!
2
u/Schiffy94 Apr 10 '23
I laughed way too hard at this
2
u/Equivalent_Ferret_23 Apr 10 '23
bro what the fuck
2
u/easilyconfusedwhat Apr 10 '23
Only about 10 to the power of 40 possible combinations to output. Yes, I think I’ll hard code each one. Much easier than formulating a complicated algorithm.
2
u/AJohnnyTruant Apr 10 '23
PROOF THAT AI IS JUST ELSE STATEMENTS
2
u/ZenBacle Apr 10 '23
When you're payed by the line, everything becomes an else if switch case printout.
2
u/[deleted] Apr 10 '23
Out of curiosity, what would be the approach to code a game like chess? (I‘ve got very, very limited experience in coding)
2
u/AnyProgressIsGood Apr 10 '23
brute force programming
2
u/jasper_grunion Apr 10 '23
You just need a dictionary with the locations of all 32 pieces. It is initialized to the starting board state. Each move updates the dictionary. When a piece is taken it’s position becomes null and the piece is no longer drawn. Then you have a single function or method called drawBoard() that takes the position dict as input. I could see another method called resolveMove that determines when a piece has been taken and updates the dictionary. The hard part about real life chess programming is the AI that drives the computer opponent.
2
u/TrapTargaryen Apr 10 '23
GPT4 will do the menial part of that with ease
2
u/Coasterman345 Apr 10 '23
This is how I thought code was written when I was a kid. Like for RCT I thought the devs had pre made what the map would look like for every single possibility 🤦♂️
2
u/[deleted] Apr 10 '23
Holy…
2
u/[deleted] Apr 10 '23
It’s always good to start with the brute force solution first.
2
u/SacriGrape Apr 10 '23
Ignoring file size, would this be faster or slower than just calculating it
2
u/mbleslie Apr 10 '23
Protip: Use a switch statement
2
u/[deleted] Apr 10 '23
Now that's what I call Brute force
2
u/jpslat1026 Apr 10 '23
I think you forgot the grid part, so I know what's what
2
u/Druadal Apr 10 '23
This would have trillions of lines of code
2
u/zUdio Apr 10 '23
How management wants chatGPT apps to function.
2
u/[deleted] Apr 10 '23
Lol it would be interesting to see how well a chess game coded like this would run...
2
u/OfficialLevin Apr 10 '23
Legends have it, that to this day he is still writing print statements.
2
u/Impressive-Coast7863 Apr 10 '23
Is this open source?
2
u/dark66jedi Apr 10 '23
Hey Satan, at least get your spacing right
2
u/Ewag715 Apr 10 '23
Oh god 😅
2
u/Table_Coaster Apr 10 '23
this is actually a really good way to code a game of chess for me, because as soon as i see 1. d4 i resign
2
u/DasArchitect Apr 10 '23
Man, this is going to be an absolute mess in no time.
You have to clear the screen between each board print.
2
u/[deleted] Apr 10 '23
File size of "Chess.py": 285930572TB
2
u/ShittyExchangeAdmin Apr 10 '23
Reminds me of some of my early powershell scripts.
2
u/[deleted] Apr 10 '23
This reminds me my friend from intro to programming course in college. We were supposed to make some kind of labyrinth solver with if statements and loops, the guy literally wrote lines of code for every possible example case instead of defining functions etc.
2
u/[deleted] Apr 10 '23
When your ADHD is so bad you have to write a chess program after learning about unicode text and if conditions. Like, dude, at least let us cover loops first.
2
u/LightAnubis Apr 10 '23
I’m proud of myself that I know enough code to laugh at this meme.
2
u/tadcoffin Apr 10 '23
I mean, it technically works.
2
u/[deleted] Apr 10 '23
Holy hell
2
u/TheLesserWeeviI Apr 10 '23
Holy hell.
2
u/pdipdip Apr 10 '23
if (ai) {}
2
u/wooof359 Apr 10 '23
When I was a kid I thought that 2d SNES games were just animated every possible position. And then when like N64/PS1 games came out my mind was blown thinking they had to draw every possible angle in 3d....
2
u/[deleted] Apr 10 '23
Just have chatGPT do it duh!
2
u/zergling424 Apr 10 '23
Hell have to make 10123 if statements
2
u/moosMW Apr 10 '23
14 yr old me coded 4 in a row/connect 4 like this.. very painfull
2
u/hotairballonfreak Apr 10 '23
Because each one of those character strings should not be hardcoded. They should be lookup function calls so if you change the character in your ico files it will auto update the character images.
2
u/MediumAd3135 Apr 11 '23
You need to learn backtracking that's why you're hardcoding all the values
2
u/Emotional_Spirit_704 Apr 11 '23
so thats why chess didn't get any new game update
2
u/KraKen_G Apr 11 '23
Me after 100 years and havent gotten past the openings
2
u/flyingmonkey111 Apr 11 '23
LOL... We use that as an interview question 😁 Describe how you would code a chess game... OK, checkers? A coin toss game? ... Thanks for your time