Python Bot Plays Connect Four (using Minimax and some heuristics)

  Рет қаралды 17,756

Games Computers Play

Games Computers Play

Күн бұрын

Пікірлер: 98
@seporokey
@seporokey 3 жыл бұрын
I really like your content. It's exactly what I'm doing to practice game dev. I've been recreating pen and paper games with AI bots. I want to try something like Spades next since I want to try and make an AI that plays a game with incomplete information.
@gskartwii
@gskartwii 3 жыл бұрын
I have also been thinking about card game AI a lot, spades would be amazing as well as hearts. I would also love to see a video about that!
@ltsMeNoodle
@ltsMeNoodle 3 жыл бұрын
Thanks for the idea! I think I might try making a hearts bot.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Thanks, looked it up. Yes, I think AI for a trick-taking game, like Spades should be a fun thing to program. I am learning about quite a few great games from the comments!
@mnzznxplay9747
@mnzznxplay9747 3 жыл бұрын
I remember when I was living in England we had c4 contest in our class. Rules were that first who will win three times is winner of match. I climbed up to the finale, then lost 2 times, but came back winning twice. Last, decisive match was intence, it led to position where me and opponent needed the same spot filled to win in empty column. I analysed this and came to conclusion that my opponent should be first to place circle here for my win. Also, if we weren't going to win on rest of field, she will be first to put circle in that column, so I just should not let her win on rest of the field. We filled it in such way that would not give anyone victory. And finally we came to that column. As I predicted, she put her circle first, then me. My 4-circle row was finally filled. Victory
@rauljvila
@rauljvila 3 жыл бұрын
Excellent explanation! Now with minimax and heuristics the game has moved to the next level. I look forward to the next projects. Thanks for the videos, and especially for all the links to other resources.
@dhillaz
@dhillaz 3 жыл бұрын
18:35 Wow, only when you said that did I realize how new your channel is. Your videos are to the standard of a much more established creator. I will be sure to hit the like button more often, so you get where you need to be. 👍
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Thanks, I really appreciate it. It's a bit of a stress knowing your videos get thousands of views (as opposed to how chill it was before, when not even my wife watched my videos), but it is a good, exciting stress!
@gorillaau
@gorillaau 3 жыл бұрын
@@GamesComputersPlay Does your wife watches you videos these days? :-)
@DanielBerke
@DanielBerke 3 жыл бұрын
That's really neat. I love the visual representation of the game state in the code output. Also the visual puns for the game names were pretty funny. :) (As a Python programmer interested in learning a faster language myself, I've been looking into the Rust language recently.)
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Thanks. I did hear about Rust several times already: like it is as fast as C++, but much more friendlier, almost like Python. Sounds very promising.
@sofia.eris.bauhaus
@sofia.eris.bauhaus 3 жыл бұрын
1988, when men still were small disks of plastic. nowadays we don't have REAL MEN like this anymore 😔.
@linkhorshaw9575
@linkhorshaw9575 3 жыл бұрын
You should ask codebullet to do a face off
@underrated1524
@underrated1524 3 жыл бұрын
YES (Just in general, I would love to see a collab between Games Computers Play and the big CB.)
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
His avatar images are definitely better. I would argue I make better PPTs???.... ¯\_(ツ)_/¯
@MrConverse
@MrConverse 3 жыл бұрын
Loser does a face reveal?
@lizzzylavender
@lizzzylavender 3 жыл бұрын
“It was actually solved by two guys, just a couple of weeks apart” Guys I think we found the reincarnations of Leibniz and Newton
@Chloe-ju7jp
@Chloe-ju7jp 3 жыл бұрын
you should face it off against a perfect bot... See how often it manages to win as first player. Or as second player with the perfect bot being forced to start in columns 1,2,6, or 7
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Just did that (manually, entering each move in the code), increasing the depth to 8 plies (makes it think a few seconds per move). As you can guess, my bot lost, but the fight it put up was better than I expected. Lost on the ply 32, with only 10 empty cells to go. One of those situations where there are deep "wells" on the board, and you just forced to make your move there. If I understand correctly how it works, there are a very special moves in the beginning red has to follow to force the victory. And this special beginning only can be deducted with very deep minimax analysis - (almost the whole game, 41 plies, if I understand correectly). So my puny little 8 plies are no match. Heuristics helps a little, but it's like having a good form for your deadlift while competing with a forklift.
@Chloe-ju7jp
@Chloe-ju7jp 3 жыл бұрын
@@GamesComputersPlay very interesting, thank you!
@ScorpioneOrzion
@ScorpioneOrzion 3 жыл бұрын
@@GamesComputersPlay tho the game itself is solved, as in from any given position you can tell what the result would be if both players play the best moves
@themightyripples6582
@themightyripples6582 3 жыл бұрын
I was actually just about to mention something similar. I wonder how accurate GCP's bot was in the end? How close in accuracy to the perfect bot was it?
@EebstertheGreat
@EebstertheGreat 3 жыл бұрын
"Man" is a classic name for a piece on a board game. The best-known example is chessmen. In chess, the word "piece" is often used with a technical meaning that excludes pawns (and sometimes the king). But all of them together, particularly in older writing, are "men," even the queen. It's a little silly and doesn't make much grammatical sense anymore, but it is still used from time to time.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
I honestly never heard it in this context before (given I have only been speaking English on any decent level only a third of my life, but still). I have looked it up just now in the Oxford Learner's Dictionary - and, indeed, meaning number 14: an object you play with in a game. I absolutely enjoy these little obscure meanings of well-known words, so this one is quite a gem.
@sobertillnoon
@sobertillnoon 3 жыл бұрын
F in chat for Second Guy. FeelsBadMan
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
I feel there was a story worth of a Hollywood movie. I will probably cry in the end.
@UofU10
@UofU10 3 жыл бұрын
Love it! Excellent content!
@R8Spike
@R8Spike 3 жыл бұрын
3:16 cracked me up
@AbylaiMaster
@AbylaiMaster 3 жыл бұрын
You missed E in connect four (title)
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Woopsie. Thanks, fixed! (It's almost 2 am here)
@easytiger6570
@easytiger6570 3 жыл бұрын
@@GamesComputersPlay Yekaterinburg?
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
@@easytiger6570 A bit more to the East. And to the South. Actually, not a bit, a lot. Even to another country. I'm in Shanghai :)
@easytiger6570
@easytiger6570 3 жыл бұрын
@@GamesComputersPlay But time zones doesn't match
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
@@easytiger6570 how do you mean? +8 GMT, it's 7 minutes past midnight here right now.
@choco_jack7016
@choco_jack7016 3 жыл бұрын
you remind me of code bullet, which I like because you code practically useless stuff in a really interesting way.
@choco_jack7016
@choco_jack7016 3 жыл бұрын
wait damn didn't expect you to talk about him
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Yes, it was after his videos I got the idea for this channel. He has some fun videos, totally agree.
@MoreInsane96
@MoreInsane96 3 жыл бұрын
In italy we call it "Power Four"
@renerpho
@renerpho 3 жыл бұрын
I hope Code Bullet is okay. He had taken breaks between videos before, but not without any activity on other social media for almost a year.
@ArmaRGool
@ArmaRGool 3 жыл бұрын
Great video dude
@Raf99
@Raf99 3 жыл бұрын
Dont know if its easter egg or not but you are with laptop on video and there is the same laptop in background layout. Funny :)
@Swagpion
@Swagpion 2 жыл бұрын
I want to see this A.I. go against code bullet's connect 4 A.I.
@GamesComputersPlay
@GamesComputersPlay 2 жыл бұрын
That would be quite an interesting match indeed. From what I understand, CB’s AI copies a lot of moves from “The perfect” algorithm. But it couldn’t copy it all - so it probably will be somewhat clueless after X moves. So it boils down to: can my heuristics algorithm hold on against “perfect algorithm” for X moves. IDK, too many unknowns to make a meaningful prediction. But I’d cheer for my guy, of course.
@ionutdobrin8541
@ionutdobrin8541 3 жыл бұрын
I really like your content! Love what you do. I don't know if you ever played Catan, but in case you did or you would have the time to search the game up, do you think it can be programmed and actually test some of the strategies in the game? Would really mean a lot your input and thoughts.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Thanks a lot! I heard about Settlers of Catan multiple times, never had a chance to actually play. From what I know it is a bit to complex to write a bot, but I never looked into it closely, so I can easily be wrong.
@ionutdobrin8541
@ionutdobrin8541 3 жыл бұрын
@@GamesComputersPlay yes it is a fun game and pretty complex to program imo. But i would much rather know the opinion of an expert, so if you ever have the time to study it better, i would greatly appreciate an opinion.
@tandemmints8934
@tandemmints8934 3 жыл бұрын
Yeah, it likely would be very difficult to write a bot for. There is a great deal of randomness and social dynamics involved. One of the better ways I've found to play is not to focus on so much on 'winning', but rather on just making sure others are having a good time. Do that and they are more likely to cooperate with you, which greatly increases your chances of winning. Anyways, I love the game and if you need people to play with to get a feel for it I would be up for it!
@Bleuthatup
@Bleuthatup 3 жыл бұрын
Comentando para bumpearte el engagement. Gran canal. Suscribed
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Muchas gracias! "Bumpeare" e una palabra muy divertido!
@arkadye
@arkadye 3 жыл бұрын
Watching this I see two things that could make your bot a lot more competitive. A transposition table is fairly easy to implement and should save you a lot of time, as I think you're analysing the same position multiple times. It's particularly powerful in a game like Connect 4 where the number of moves is quite limited. The other thing is an "opening book" or database for the first few ply so you can always play the best couple of moves at the start.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Yes, transpositional table was one of the things in the "perfect bot". I actually found that "perfect bot" tutorial when I finished the program and most of the script, so didn't have a chance to implement all those technics. You can say it was an experiment how far I could go by myself without using anything, except wikipedia. I tried to implement "dynamic programming" (which is not exactly the same thing, but kind of solving the same problem), but it didn't help much, so I abandoned the idea. "Open book" is a good idea, practically, but I don't like it on some other level. It almost feels like cheating, like the program gets the moves off of a cheat sheet...
@queuebit
@queuebit 3 жыл бұрын
Thoughts on building an AI / simluator for card games (like Euchre, Hearts, etc)?
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Yes, some of these are in the to do list. I actually simulated a kids card game called "War" - it was quite easy as it reauires zero decisions from playes. You can see what rules make it less likely to get stuck in a cycle and so on. Probably not enough material to put it in a video, but it was an interesting mini-project.
@opaknack7249
@opaknack7249 3 жыл бұрын
Very cool Dude making very cool vids
@tctrainconstruct2592
@tctrainconstruct2592 3 жыл бұрын
11:18 what about noughts and crosses? btw you could also have employed move ordering, transposition table, a bitboard, forward pruning (like ProbCut), CNS/PNS, an opening book and zugzwang evaluation. In the heuristic, you could also add in evaluation for dead columns and upcoming zugzwangs.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Absolutely. But this was more like "Can I beat the game with as amature solution as possible?"
@hisho_isa
@hisho_isa 3 жыл бұрын
Good vid I enjoy
@brayanlondono_
@brayanlondono_ 3 жыл бұрын
Can you do battleship??!?
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Yes, absolutely in the list of games to try to write a bot for!
@ayior
@ayior 3 жыл бұрын
I made a comment on one of your videos predictng that you were picked up by the algorithm then. I failed you, I'm sorry, I hope the growth you deserve will come soon
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Hey, don't kick yourself too hard. To be fair, I do take some long pauses between videos, that algorythm frowns upon. And Actually I am quite happy with the crowd I already have :)
@Taterzz
@Taterzz 3 жыл бұрын
so assuming google translate is accurate, that would make you кодовая метка
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Haha, pretty close. It's more like кодовая пуля (метка is more like a mark, maybe like a bullet-point, I would say).
@a-lil-ghoti
@a-lil-ghoti 2 жыл бұрын
I'm surprised you didn't try estimating the strength of the bots by reducing the search depth of your algorithm. Like if you don't even use minimax but use your heuristic, can you beat the medium bot? If not, how deep would you have to search? And how much did the heuristic help? What about other heuristics, even silly things like "Checkers in the middle are worth more than at the edges". If you follow up I'd be interested!
@Komeuppance
@Komeuppance 3 жыл бұрын
"Perchild" lol... is that even good enough for 2021?
@stopitnowlol6697
@stopitnowlol6697 3 жыл бұрын
Could you do a coding tutorial? like your next project show how you wrote the code and a normal vid like this. Something like this where you use image recognition and stuff like that would be cool to learn.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
I'm afraid it would be one rather boring video. :) Also, and more importantly, as not a professional programmer, I do have a bunch of "bad habits" in the way I code, so I wouldn't recommend myself as a coding teacher. Picking up fun projects, creative problem-solving, can-do attitude (and of course modesty) - I may have these things, but not the flawless coding style. What is actually a good thing for me to do is to provide more comments in the code, so anybody can follow along more easily. I often forget that this is something I may later publish and cut corners more often than a good programmer should.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
I have updated the source code with a bunch of comments - should be more or less easy to figure out what's what now.
@stopitnowlol6697
@stopitnowlol6697 3 жыл бұрын
@@GamesComputersPlay thank you!
@NovemberOrWhatever
@NovemberOrWhatever 3 жыл бұрын
I downloaded the code, and I'll see if I can make it play better in less time. I don't know OO though, so I'm converting it to functional.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Sounds great. Let me know what result you manage to achieve! As for the code, there is definitely room for improvement. Here's what I can think of right now: - Better way of storing the data. I use string for positions, which was simple and easy. I'm pretty sure if you somehow encode it it numbers it can get much easier. - A way to speed up the whole thing - numba, just sugegsted in one of the comments (thank you to who suggested it). - Another comment (thanks to the author) suggeted transporation table and "openings book" - which definitely should have positiove effect. - Any other tricks the Pascal Pons'es tutorial has. I only implemented basic ones, so there is more. - Personally, if I haven't read Perfect Bot's manual (and didn't know that the game was solved), my next step would have been exploring the option of combining minimax and heuristic scoring. Score all the nodes and work out the way to highest scores.
@NovemberOrWhatever
@NovemberOrWhatever 3 жыл бұрын
@@GamesComputersPlay Converting OO to functional (or at least mostly functional) is hard. I haven't spent too much time working on it so far, but I'm making some headway. I'm averaging .002 seconds per turn despite not using @jit, so I'm pretty sure it's not doing as much calculation as it should be.
@helloiamenergyman
@helloiamenergyman 3 жыл бұрын
Nice :)
@arandomyoutubenerd276
@arandomyoutubenerd276 3 жыл бұрын
Pog! This is really interesting! Sorry to ask you again but are you able to help us with a Risk program me and some others are trying to make?
@moonflocookie
@moonflocookie 3 жыл бұрын
What is your problem?
@arandomyoutubenerd276
@arandomyoutubenerd276 3 жыл бұрын
@@moonflocookie a lot lol, do you have Discord?
@laytonjr6601
@laytonjr6601 3 жыл бұрын
The game is much more complex to modelise than monopoly, good luck!
@arandomyoutubenerd276
@arandomyoutubenerd276 3 жыл бұрын
@@laytonjr6601 yah Thanks
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Risk is definitely in the backlog, although to be honest, right now I don'twouldn't even know where to begin. The very first part - where players have to place their pieces is already very confusing.
@antcubingx
@antcubingx 3 жыл бұрын
Hi! can you make a video about uno! I love your videos!
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Thanks a lot. Yes, quite a few people suggested Uno. I actually still need to learn to how to play it myself, as I have never played it Anyway, it definitely goes in the "to-do" list of projects!
@snellface
@snellface 3 жыл бұрын
****** you are welcome, great vidoes btw ****** :p
@nathanb011
@nathanb011 3 жыл бұрын
I think you would find Numba rather useful.
@GamesComputersPlay
@GamesComputersPlay 3 жыл бұрын
Thanks! Haven't heard of it, Googled just now. It does look really promising, especially the part where you don't really need to change your code much. Should give it a go in one of the futuree projects.
@nathanb011
@nathanb011 3 жыл бұрын
@@GamesComputersPlay nothing beats native code, but it is extremely useful 👍
@DIGGERfromAR
@DIGGERfromAR 2 жыл бұрын
Remove the unnecessary virtue signaling.
@GamesComputersPlay
@GamesComputersPlay 2 жыл бұрын
And I didn't even get to the the whole "white move first" controversy.
@yoshi6421
@yoshi6421 2 жыл бұрын
Obviously didn't pause
“Don’t stop the chances.”
00:44
ISSEI / いっせい
Рет қаралды 62 МЛН
Леон киллер и Оля Полякова 😹
00:42
Канал Смеха
Рет қаралды 4,7 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
What P vs NP is actually about
17:58
Polylog
Рет қаралды 149 М.
Oh, wait, actually the best Wordle opener is not “crane”…
10:53
Sudoku Solver in Python (using 10 different solving strategies)
18:22
Games Computers Play
Рет қаралды 38 М.
I built my own 16-Bit CPU in Excel
15:45
Inkbox
Рет қаралды 1,7 МЛН
how NASA writes space-proof code
6:03
Low Level
Рет қаралды 2,4 МЛН
Coding Adventure: Chess
29:22
Sebastian Lague
Рет қаралды 3,9 МЛН
Bot destroys Tetris (or at least plays better than an average human)
19:26
Games Computers Play
Рет қаралды 14 М.