For some languages, it can produce really phenomenal results. Python, it usually has a pretty good answer for well defined problems. The problem with this scenario is that it was trained by an absolute ton of people learning to program by making the same thing.
It's about 50/50 whether it saves me a second of typing on any given suggestion. The deeper into the guts of something I am, the less likely it is to be useful. With Dart, it tends to mangle syntax and leave me doing more work than if I'd just finished typing.
For me, copilot has been mostly a very smart auto-complete. Sometimes as I'm writing a method header, it'll just complete the entire function exactly as I need it, often taking context in from the rest of the file or even occasionally from surrounding files. It's been totally fine just running in the background and helping occasionally with semi-repetitive tasks.
But also, that's not the only problem with this code. It changes active player before displaying the winner, which means it will display the wrong winner. And I'm sure there's more that I'm missing / forgot.
Honestly. this is exactly how I make silly little terminal games in python. very few functions. mixing mvc. write as much as you can in one go and fix the problems as you find them. TDD be damned.
To be fair, there are only 8 win conditions and each one only takes up one line here.
I recently had to write tic tac toe in IEC structured text for school which has no tuples, iterators, or anything to make it more consice and this was pretty much the only way.
I’m looking at it and there’s an ID statement for player == 1. Then immediately afterwards, assign 1 to player. Redundant isn’t it? Am I missing something?
A neat way of avoiding this if statement would be to have player_1 == 1 and player_2 == -1. At the end of each turn, multiply the number by -1.
I'd imagine it doesn't make much of a difference here (I mean if you really cared about performance you wouldn't be using Python), but "if" statements can screw up pipelining, which, for not-so-trivial programs, can dramatically slow down execution.
This also works, but the modulus operator involves division. If the compiler doesn't recognize %2 as logically equivalent to a bitwise operation on the least significant bit then this might slow things down.
Multiplication is as simple as several adders chained together with some bit-shifting for their inputs, and you could somewhat easily implement this algorithm entirely in hardware such that any two 64 bit ints could be multiplied together in a single cycle (floats would be slightly more complicated, but more or less the same).
Meanwhile, division is very complicated (relative to the other 3 fundamental arithmetic operations you learn in grade school, anyway), and there are many different algorithms out there which can get the job done. The naive approaches converge linearly, and some of the more interesting ones do it quadratically, but in any case division is likely to be a multi-cycle operation. In other words, slow.
You could cut the corners here and there and make the code prettier, but on the other hand its not like tic tac toe is going to change its rules anytime soon, and its readable enough
It looks like value for each position on the board defaults to 0, than changes to 1 or 2 depending on which player put a mark in that location.
At the beginning of the game, all spaces would be set to 0, which would mean that the statement checking for a win would be valid right from the beginning.
The memories! I had a uni lab for tic tac toe. It mentioned there would be a surprise announcement a week before the due date for bonus marks. The announcement was that for bonus marks we had to submit 4x4(winning 4 in a row) and 5x5 (winning 3 in a row) implementation
If you had built it like the above the code didn’t scale well at all. If you had built it well it was basically just changing some constants
Any Python program that uses input() is a sign that it's a homework / class assignment. It's just someone learning the language / learning to program in general. Not really funny.
If you want a proper terminal UI, you'd be using curses at least, or maybe termbox or prompt_toolkit.
There are very few situations in any application development where you would do the kind of interaction with user that's implied by input() -> print(). If you need to read stdin, then you'll invariably end up at least accessing sys.stdin instead of calling input() because of issues with buffering, or maybe wanting realdline-like functionality, or displaying completion suggestions... input() is too simplistic for real work.
119
u/nekokattt May 11 '22
just make a list of tuples of the coordinates you want to check and iterate over it.
Then look into f-strings instead of string concatenation.
46
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 11 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
25
u/frugalerthingsinlife May 12 '22
Honestly. this is exactly how I make silly little terminal games in python. very few functions. mixing mvc. write as much as you can in one go and fix the problems as you find them. TDD be damned.
19
u/waterbyseth May 12 '22
To be fair, there are only 8 win conditions and each one only takes up one line here.
I recently had to write tic tac toe in IEC structured text for school which has no tuples, iterators, or anything to make it more consice and this was pretty much the only way.
18
u/randomsmiteplayer May 11 '22
I’m looking at it and there’s an ID statement for player == 1. Then immediately afterwards, assign 1 to player. Redundant isn’t it? Am I missing something?
1
u/RoLoLoLoLo May 12 '22
Does it though? Exercise to the reader: what happens in case of a tie? What happens when two players enter the same number?
3
u/Abstal_223 May 12 '22
OP got that yandere dev type code
4
u/trutheality May 12 '22
According to this in the event of a tie the last player to go wins?
2
u/[deleted] May 12 '22
Why is this nsfw
1
u/kristallnachte May 12 '22
But what did it cost?
1
u/SlyWigga May 12 '22
You could cut the corners here and there and make the code prettier, but on the other hand its not like tic tac toe is going to change its rules anytime soon, and its readable enough
1
u/havens1515 May 12 '22
Does it work, though?
It looks like value for each position on the board defaults to 0, than changes to 1 or 2 depending on which player put a mark in that location.
At the beginning of the game, all spaces would be set to 0, which would mean that the statement checking for a win would be valid right from the beginning.
Unless I'm missing something here...
1
u/Beach-Appropriate May 13 '22
The memories! I had a uni lab for tic tac toe. It mentioned there would be a surprise announcement a week before the due date for bonus marks. The announcement was that for bonus marks we had to submit 4x4(winning 4 in a row) and 5x5 (winning 3 in a row) implementation
If you had built it like the above the code didn’t scale well at all. If you had built it well it was basically just changing some constants
1
u/TheGlamorousWonkyApe May 22 '22
can see why it was marked nsfw
1
u/Kakao_Trees May 12 '22
eh, beginner code gotta start with this, not too bad for a new guy
1
u/Tm1lly May 12 '22
Lolol whoever made this not safe for work 😂
0
u/[deleted] May 13 '22
Any Python program that uses
input()is a sign that it's a homework / class assignment. It's just someone learning the language / learning to program in general. Not really funny.