r/learnpython • • Nov 17 '21

I made a Tic-Tac-Toe game with 3642 lines of code and I'm sorry.

I posted on this subreddit asking for help on a tic tac toe game that was 3642 lines of code and I got flamed (in a good way).

Thank you.

I had sculped an effigy of inefficiency and I have been ushered into the world with a fresh perspective. I did my best and reduced the code by a factor of 10 to 380 lines of code. It most likely can be cleaned up further but it did the job.

Here's the code. I believe it is impossible to beat. Give it a try, if you win tell me.

Thanks for the help guys.

https://github.com/M0pps/Tic-Tac-Toe.py.git

657 Upvotes

180 comments sorted by

12

u/Muff_in_the_Mule Nov 18 '21

That's actually pretty impressive, how did you not get completely lost trying to navigate all that code?

One of my first projects I made a Super tic-tac-tac (each grid square has a grid inside and influences the larger grid play order) with a simple AI in about 350 lines and lots of that was me drawing out the grid manually which is really really inefficient.

4

u/M0pps Nov 18 '21

I basically had "turns" in a while loop and I simply looked for the "turns" and had a lot of scrolling.

1

u/[deleted] Nov 18 '21

[removed] — view removed comment

1

u/M0pps Nov 18 '21

I dint own a monitor. I have a gaming laptop with 15.6 inch screen

24

u/xelf Elf Nov 18 '21 edited Nov 18 '21

It's a bit unfair to say you got flamed. You said the code was broken and asked for help and you got it.

10

u/M0pps Nov 18 '21

Nah, it was ironic, flamed in the good way. Not upset!

9

u/xelf Elf Nov 18 '21

Ah good. We're here to help. I would have stepped on anyone if they were actually impolite. =)

edit, I see your edit now, Thank you!

14

u/xelf Elf Nov 18 '21 edited Nov 18 '21

Also: Don't feel bad, here's 18,000 lines of TTT code:

(note the author!)

And here's 1 line of TTT:

(lambda: [[[list(iter(lambda: ((userinput() and False or check_state() == -1 and [state[y].__setitem__(x, 2) for x, y in [aimove()]]) and False or check_state() != -1), True)) and display(False) or print(["Draw!", "You won!", "Computer won!"][check_state()]) for userinput, aimove in [[lambda: [(list(iter(lambda: (display(True) or xin.__setitem__(0, input(">> ")) or True) and xin[0] in valid, True)) and False) or [state[y].__setitem__(x, 1) for x, y in [num_to_cell(int(xin[0]))]] for xin, valid in [([""], [str(cell_to_num(j, i)) for i in range(3) for j in range(3) if state[i][j] == 0])]], lambda: ([(k, j) for i in [2, 1] for j in range(3) for k in range(3) for l in range(4) if (l == 0 and state[j][k] == 0 and state[j][(k+1)%3] == i and state[j][(k+2)%3] == i) or (l == 1 and state[j][k] == 0 and state[(j+1)%3][k] == i and state[(j+2)%3][k] == i) or (l == 2 and j == k and state[j][k] == 0 and state[(j+1)%3][(k+1)%3] == i and state[(j+2)%3][(k+2)%3] == i) or (l == 3 and j + k == 2 and state[j][k] == 0 and state[(j+1)%3][(k+2)%3] == i and state[(j+2)%3][(k+1)%3] == i)] + ([(1, 1)] if True and state[1][1] == 0 else []) + [list(iter(lambda: (value[0] is not None and state[value[0][1]][value[0][0]] == 0 or value.__setitem__(0, (random(), random())) and False), True)) and False or value[0] for value, random in [([None], lambda: __import__("random").randint(0, 2))]])[0]]]] for display, check_state in [[lambda numbers: print("\033c" if ansi else "", end='') or ([print(("---+---+---\n" if i > 0 else "") + "|".join([" " + (("\033[36m" if ansi else "") + str(cell_to_num(j, i)) + ("\033[0m" if ansi else "") if numbers and x == 0 else " XO"[x]) + " " for j, x in enumerate(state[i])])) for i in range(3)] and None), lambda: ([row[0] for row in state if row == [1] * 3 or row == [2] * 3] or [col[0] for col in zip(*state) if col == (1,) * 3 or col == (2,) * 3] or ([state[0][0]] if state[0][0] == state[1][1] == state[2][2] != 0 else []) or ([state[2][0]] if state[2][0] == state[1][1] == state[0][2] != 0 else []) or [-1 if any([state[y][x] == 0 for x in range(3) for y in range(3)]) else 0])[0]]]] for state, num_to_cell, cell_to_num, ansi in [([[0 for _ in range(3)] for _ in range(3)], (lambda num: ((num - 1) % 3, 2 - (num - 1) // 3)), (lambda x, y: (2 - y) * 3 + x + 1), True)]] and None)()

Bit of a messy code golf obfuscation. Terrible, but 1 line.

edit, looks like he updated and shrank the ttt code to 5k ish, you can still see the 18k version in the git history, link below

10

u/mandradon Nov 18 '21

18,000 lines! Holy cow!

That poor programmer. I sure hope someone told them about functions.

19

u/xelf Elf Nov 18 '21

Maybe someone can suggest a book for him to read. =D

like the one he wrote...

In case you missed it, that was Al Sweigart, and the 18k lines were generated by a program he wrote to write a tic tac toe program.

8

u/mandradon Nov 18 '21

Oh, hahaha. I'll see myself out.

I didn't realize that. That's amazing then. Al is a rockstar.

4

u/[deleted] Nov 18 '21

[removed] — view removed comment

2

u/mandradon Nov 18 '21

Yeah, I didn't realize it was by Al. :)

An exercise in insanity! I can't imagine doing all of that.

2

u/talltime Nov 18 '21

I mean it says 5195 right there.

3

u/xelf Elf Nov 18 '21

Bah, looks like he updated it. Note all the checkin comments "made the code shorter". I think it was more fun at 18k. Anyway, thanks to the wonders of git, you can still see the original.

https://github.com/asweigart/my_first_tic_tac_toe/blob/9f38b04e857426c5a5b80919ad0b5fce0947c022/tictactoe.py

18205 lines

3

u/JacksonDonaldson Nov 18 '21

Here's the code. I believe it is impossible to beat. Give it a try, if you win tell me.

Mate, I found a way to win.Winning combo:5,1,7,3

2

u/M0pps Nov 18 '21

Proved me wrong! :) The if statements to cover user winning moves was straight from the dome so I was bound to miss some.

533

u/PhantomlelsIII Nov 17 '21

Im honestly impressed that you had the patience to write 3600 lines of code as like one of your first few projects. Keep that dedication man.

145

u/DrMaxwellEdison Django and stuff Nov 18 '21

In my youth, I would do some programs like that. Copy-paste-edit seemed easier than designing reusable methods, so you take one 50-line block and start exponentially duplicating it into a monstrosity.

15

u/SvG_Pheonix Nov 18 '21

Haha I’m glad to know I wasn’t the only one

21

u/DezXerneas Nov 18 '21

Yep, used to do the same. It's the only way to learn tbh.

1

u/Avi8torAce Nov 20 '21

In my youth, I used to write hundred lines of code in BASIC to simply get a face with a blinky eye on my CRT TV to then for it all to be lost once I powered off my Radio Shack Tandy TRS-80 computer…. and just like that, repeat it again in a couple of weeks. 🤦🏼‍♂️😂

1

u/[deleted] Dec 07 '21

Sounds like Sisyphus with extra steps

92

u/bumpkinspicefatte Nov 17 '21

That might be a bit excessive, but don't be apologetic, you needed to start somewhere and you stuck the landing. Good job!

In due time, and as long as you keep coding, you'll look back at this code, and possibly have an urge to rewrite it, and next thing you know it you'll size it down by half or more.

And it'll keep getting smaller, smaller, and smaller the more you learn.

Keep it up!

14

u/Xzenor Nov 18 '21

And eventually, it's a one-liner!

7

u/[deleted] Nov 18 '21

[removed] — view removed comment

2

u/Xzenor Nov 18 '21

That actually doesn't explain it at all...

56

u/[deleted] Nov 17 '21

Good job on paring it down, but I am slightly disappointed I didn't get to see the behemoth code. :(

258

u/naclmolecule Nov 17 '21

How about 6 lines?

from itertools import*;m,p,q,r,s,l,a=dict(enumerate("276951438")),[[],[]],0,print,' │ │ ','─┼─┼─','XO';b=[*map(list,[s,l,s,l,s])];f=lambda:r('\n'.join(map(''.join,b)));f()
while m:
 i=int(input(a[q]+' move: '))-1;p[q]+=[int(m.pop(i))];b[~(i//3*2)][i%3*2]=a[q];f()
 if any(sum(d)==15for d in combinations(p[q],3)):r(a[q]+' wins');break
 q^=1
else:r('Draw')

145

u/PhantomlelsIII Nov 17 '21

How in the actual fuck does this work

189

u/tylerthehun Nov 17 '21

"276951438"

if any(sum(d)==15

This is the secret sauce right here. Tic-tac-toe, as a game, is isomorphic to one where players take turns picking numbers, and whoever can add three of theirs up to 15 first wins. As long as you map those numbers to the right spaces on the traditional tic-tac-toe grid, you can forget all about checking for full, straight lines of matching symbols, and just do a bit of quik mafs to see if anybody wins.

Granted, doing that all in 6 lines still involves some crazy code golf wizardry...

61

u/PhantomlelsIII Nov 18 '21

wait WHAT. It is just a number adding game???? My life has changed

85

u/bumpkinspicefatte Nov 18 '21 edited Nov 18 '21

On a somewhat similar note of finding patterns while coding:

I found that when you're coding the game rock, paper, scissors, the win condition can be +2 (or -1) of your selection's index, and the loss condition can be +1 (or -2) of your selection's index.

Consider the following:

game_board = ['rock', 'paper', 'scissors']

Let's say you chose 'rock', which is game_board[0].

Your win condition is if the opponent selects game_board[2], which is 'scissors'.

Your loss condition is if the opponent selects game_board[1], which is 'paper'.

So instead of going through the hoops of writing out a fairly lengthy if statement like:

if player == opponent:
    # tie condition
elif player == 'rock' and opponent == 'paper':
    # loss condition
elif player == 'rock' and 'opponent' == 'scissors':
    # win condition
elif player == 'paper' and opponent == 'scissors':
    # loss condition
elif player == 'paper' and 'opponent' == 'rock':
    # win condition
elif player == 'scissors' and opponent == 'rock':
    # loss condition
elif player == 'scissors' and 'opponent' == 'paper':
    # win condition

You could possibly write it:

  win_condition = game_board[(player + 2) % 3]
  lose_condition = game_board[(player + 1) % 3]

And then do with what you may, whether another (but more condensed) if statement, a return, or what not.

The modulo helps wrap the arithmetic back around the list, so you don't get the index out of range error.

I thought that was pretty cool to learn.

Edit: Fixed some typos

48

u/goinROGUEin10 Nov 18 '21

So essentially every problem can be boiled down to simple mathematics by most def?

59

u/JasonDJ Nov 18 '21

Consider the following:

42.

12

u/FlatProtrusion Nov 18 '21

So that's why 42 is the answer. TIL.

6

u/[deleted] Nov 18 '21

All games are math. Check NP Complete

11

u/SapientSloth4tw Nov 18 '21

I wouldn’t say every problem, but a lot of simple games like this have math based solutions

4

u/yokingato Nov 18 '21

The mighty mos

4

u/Rhyme_like_dime Nov 18 '21

Mos Def writes the best functions tbh

2

u/ledepression Nov 18 '21

It's the central point of coding

1

u/Jeklah Nov 18 '21

Essentially, yes.

edit

see game theory, p vs np..some heavy reading/maths but v interesting.

1

u/wsppan Nov 18 '21

Yes. This is then expanded on by using data structures and algorithms to solve efficiently (time and space concerns)

1

u/_E8_ Nov 18 '21

You're are aware code is binary, right?

2

u/Jeklah Nov 18 '21

This is a very interesting insight. Thanks for this.

0

u/[deleted] Nov 18 '21

[deleted]

0

u/_E8_ Nov 18 '21

What is a 'ring'? (group theory)

1

u/[deleted] Dec 07 '21

Always has been

3

u/ReverseCaptioningBot Dec 07 '21

Always has been

this has been an accessibility service from your friendly neighborhood bot

10

u/PhantomlelsIII Nov 18 '21

Wait but what if you do 1, 4, and 7? They add up 12, but it is a win?

40

u/ct075 Nov 18 '21

The numbers aren't traditional positions, but rather the numbers on a magic square.

Notice that 276951438 is exactly the order of the numbers in the article.

7

u/PhantomlelsIII Nov 18 '21

Oh thats interesting. Thanks! I was wondering what was up with the weird number setup lol.

3

u/tylerthehun Nov 18 '21

It shouldn't be, no. If it is, you numbered your grid wrong. Following the "276951438" ordering would put 1 at center-right, 4 at bottom-left, and 7 at top-center, which don't line up.

4

u/PhantomlelsIII Nov 18 '21

Yeah I didnt realize that it was a magic square thing

5

u/oakteaphone Nov 18 '21

Wait but what if you do 1, 4, and 7? They add up 12, but it is a win?

_ X _

_ _ X

X _ _

No, that's not a win!

6

u/PhantomlelsIII Nov 18 '21

Lol I didnt realize that it was magic square numbers

4

u/justingolden21 Nov 18 '21

276

951

438

To help myself visually here...

3

u/sleep-enjoyer Nov 18 '21

They did the monster math

2

u/justingolden21 Nov 18 '21

But in this example you can have 7,8,9 and still not win, right? And your opponent may not have won by then either. Or if it's the total, the moves 7,8,9 could be played and the game would also have no winner.

1

u/tylerthehun Nov 18 '21

7+8+9 doesn't add up to 15, so no, that's not a win. They also don't line up on the grid; it's not a standard numeric ordering like on a keypad, it needs to be arranged in a "magic square", as another user pointed out.

There's a reason OP used specifically "276951438" instead of just "123456789".

1

u/justingolden21 Nov 20 '21

Oh OP meant EXACTLY 15... I was wondering why I couldn't figure it out. Thanks.

2

u/wsppan Nov 18 '21

This is where studying computer science will come in handy. Learning the syntax of a programming language will only get you so far. Forget learning how to write code. That is the easy part. Learn how to solve problems using the language of the problem space. Study data structures and algorithms and the correct programming language for the particular domain where your problem arises will present itself.

1

u/krnr Nov 18 '21 edited Nov 18 '21

but you can't do the simple math!

276
951
438

O| | 
-+-+-
 | |
-+-+-
 | |X

O| |X
-+-+-
 |O|
-+-+-
 | |X

O| |X
-+-+-
 |O|O
-+-+-
X| |X


O|O|X  15!
-+-+-
 |O|O
-+-+-
X| |X

(markdown, ffs)

UPD: oh! sorry, i didn't see 3 the first time. then you're absolutely right.

1

u/Party-Supplies Nov 18 '21

That’s very interesting how coding tic tac toe works. We actually had a choice to make a tic tac toe game for an assignment. I didn’t end up choosing it but I’m curious if it’s consistent with the explanation you just made. I’m going to check out of curiosity.

1

u/Mean_Mud_3689 Mar 02 '24

You just saved my life. Thank you 🙏🙏🙏

0

u/[deleted] Nov 18 '21

[deleted]

2

u/SSG_SSG_BloodMoon Nov 18 '21

dict(enumerate("276951438"))

this is the order of the values

16

u/Iirkola Nov 18 '21

pff check this out

import Tic

import Tok

import Toes

1

u/RevRagnarok Nov 18 '21

This is how people try to sneak malware into OSS. ;)

40

u/Zixarr Nov 17 '21

Since the semicolon is technically a line separator, it could be argued that this is far more than 6 lines (14ish by my count).

6

u/PeaceLazer Nov 18 '21

; definitely counts as a new line. Still impressive though

3

u/notislant Nov 18 '21

I feel like my brain fried just looking at this. Extremely impressive though!

1

u/[deleted] Nov 18 '21

Damn.................. take my job, and my money.

1

u/jafarghani Nov 18 '21

just qestion from a beginner, why you used "from itertools import *" instead of "import itertools"? wouldn't it also import the whole library too?

5

u/naclmolecule Nov 18 '21

Since this is a code golf, `from itertools import*` and `combinations` is fewer bytes than `import itertools` and `itertools.combinations`.

2

u/[deleted] Nov 18 '21

from itertools import * imports all modules from itertools. It's fine if you use import itertools but whenever you want to call a function from that module you will have to start it off with itertools. which is just tedious. You could use aliasing as import itertools as itr or some other alias if you choose to, which means you must start off the function as itr.. Less tedious. However, you can remove the problem of starting off an itertools function with anything by just using from itertools import *, which imports all its functions. It's just about making life easier.

The same applies to every other function.

1

u/CraigAT Nov 18 '21

How cool. Can someone please explain what is happening in the "combinations" part, please?
I can understand enough to assume it is using the dictionary a to do a sum of each of the rows, columns and diagonals to check for the sum being 15. But how?
I presume combinations part (part of iter tools) is somehow giving all the options of the directions, but it's this bit I would like explained, please. How are the inputs to the function able to span the different directions including diagonals?
TIA

2

u/naclmolecule Nov 18 '21

Top reply explained it, but I also have an explanation in the doc string of the golf here: https://github.com/salt-die/golfs/blob/master/tictactoe.py.

1

u/CraigAT Nov 18 '21

Thanks. An awesome bit of coding on top of a very clever magic square concept/solution.

I think I need to paste into an IDE and work through some of the expressions so that I can see the types and values. Because at the moment I'm struggling to get my head around it as text.

1

u/womper9000 Nov 18 '21

Don't do this to me or anyone else here lol

1

u/evergladechris Nov 18 '21

from itertools import*;m,p,q,r,s,l,a=dict(enumerate("276951438")),[[],[]],0,print,' │ │ ','─┼─┼─','XO';b=[*map(list,[s,l,s,l,s])];f=lambda:r('\n'.join(map(''.join,b)));f()
while m:
i=int(input(a[q]+' move: '))-1;p[q]+=[int(m.pop(i))];b[~(i//3*2)][i%3*2]=a[q];f()
if any(sum(d)==15for d in combinations(p[q],3)):r(a[q]+' wins');break
q^=1
else:r('Draw')

what in the actual fuck

1

u/AgentE382 Nov 20 '21

This guy golfs.

0

u/[deleted] Nov 18 '21

[deleted]

14

u/naclmolecule Nov 18 '21

It could be allowed or disallowed depending. This is a stdlib import which is usually ok.

0

u/[deleted] Nov 18 '21

[deleted]

3

u/naclmolecule Nov 18 '21

This wouldn't be a python golf, if it was a perl script.

0

u/[deleted] Nov 18 '21

[deleted]

6

u/naclmolecule Nov 18 '21

I know you're being intentionally troll-y. But the most widely accepted rules for golfing require the language to include everything needed to make the program run. Python does not include perl. So running a perl script with python wouldn't be an acceptable golf by any standard I know.

0

u/[deleted] Nov 18 '21

[deleted]

1

u/antiproton Nov 18 '21

In short, I’m leaning into my nature as a mathematician

That's a funny way of spelling 'pedant'.

You're not trying to prove a lemma here, you're trying to lawyer around the spirit of the rules of the game which are somewhat arbitrary and vary between communities.

Which isn't really a good use of anyone's time.

0

u/[deleted] Nov 18 '21

[deleted]

→ More replies (0)

16

u/[deleted] Nov 17 '21

My very first complete program (three years ago) took me 150 or so lines of code. Now I can do it in 10. 5 if I sacrifice some readability.

It's a process, you're on the right track. Keep going.

10

u/spez_edits_thedonald Nov 18 '21

end the file name with .py so that people and computers know it's a python program

check out this page https://github.com/M0pps/Tic-Tac-Toe/blob/main/Impossible

before and after, right now there's no syntax highlighting so it's hard to read, but a simple change to .py and GitHub will know that it's python

18

u/AcousticDan Py3 Nov 18 '21

Don't listen to python people on code length. Most python devs I've met obsess over it and their code is shit to read.

3

u/SapientSloth4tw Nov 18 '21

I agree with that. Like super long one line if statements shudders

2

u/laundmo Nov 18 '21

just black that shit and rejoice!

1

u/M0pps Nov 18 '21

A couple of you were actually able to beat my so-called impossible tic tac toe game.

It has been updated for that to (hopefully) no longer happen.

1

u/scoinmile Nov 19 '21

Riding on this game... How do i reset the board when starting a new game? Somehow while im able to start a new game my board is still stuck at the first game's placement 🤣

1

u/M0pps Nov 19 '21

After a win, tie, or loss it should ask if you want to play again. If you type yes it should reset. Is that not working?

1

u/scoinmile Nov 19 '21

Yes... When i type yes a new game starts but the board doesnt reset 😥

1

u/M0pps Nov 19 '21

Are you using python 3.10?

1

u/Sora_Isekai Dec 14 '21

Oh… stares at my 6000 code monstrosity. That makes both of us😅

1

u/M0pps Dec 14 '21

What was this monstrosity trying to do?

1

u/Sora_Isekai Dec 14 '21

Discord bot game. It’s my first time coding anything outside my class assignment. It’s a Frankenstein of YouTube videos, help from other people, etc. I might of gotten a bit too ambitious lol. Im currently in the “process” of shorting it, but I have finals in a week so that’ll have to wait

3

u/justingolden21 Nov 18 '21

In summary,

elif

2

u/[deleted] Nov 18 '21

You'll get there from 3600 to 6-line guy if you keep up the dedication man. Just need to learn more python syntax and you'll be able to find new ways of improving your tictactoe.

3

u/sleep-enjoyer Nov 18 '21

A strange game. The only winning move is not to play

4

u/daneelthesane Nov 18 '21

Don't pretend you made that line up, or you would be telling a real WOPR.

1

u/Gorstag Nov 18 '21

It is really a child's game. Children make mistakes. Once they actually learn the game and reach a point where it is impossible to lose/win they have essentially improved their cognitive abilities.

1

u/laundmo Nov 18 '21

ultimate tic tac toe on the other hand is almost chess-like in complexity

1

u/Outrageous_Talk_187 Jul 07 '25

I did tic-tac-toe in 90 lines 

1

u/tensigh Nov 18 '21

Reddit CAN be a cruel place sometimes. Awesome job for your first project.

1

u/[deleted] Nov 18 '21

Am impressed and I never knew this challenge happened until I stumbled on this post.

1

u/QuidHD Nov 18 '21

Good shit! I’m working on my TicTacToe project to finish out my PCEP prep course.

1

u/tiNsLeY799 Illiterate Programmer Nov 18 '21

oh!! i also made a tic-tac-toe but in.. 86 lines

it's not that great, it's just ASCII and like, extremely primitive

1

u/Sergeant_Arcade Nov 18 '21

That title gave me a good laugh. It's alright, no one writes perfectly optimized code when they first start. At least you learned something!

1

u/SapientSloth4tw Nov 18 '21

Okay, awesomely done in the reduction of lines of code. Now the question is: can you make in AI that is fallible, but not all the time? There are some really good readings about how difficult it is to make sometimes good AI as opposed to making perfect AI/trash AI

https://www.gamedeveloper.com/programming/how-devs-are-working-to-design-game-ai-that-plays-like-a-human

1

u/InsrtOriginalUsrname Nov 18 '21

Now make one with 6000.

1

u/[deleted] Nov 18 '21

Do you have a copy of your 3642-line version? I'm just really curious.

1

u/hastethis Nov 18 '21

I miss the original code :(

1

u/sluggles Nov 18 '21

That's crazy. I wrote chess in about 1000 lines of code, and I thought I could probably condense it. I can't imagine having the patience to write that much code for tic tac toe. Good job powering through, and moreso having the gusto to refactor it down to 355.

1

u/joerod Nov 18 '21 edited Jan 05 '22

it makes sense to you though 🤷

1

u/lurking_not_working Nov 18 '21

This is how you learn to program.

1

u/Im_Not_A_Dentist Nov 18 '21

With my first few projects, this is how I learned more about coding!

Think about it - what’s one of the reasons that we code?

To make things more efficient, quicker (a quote I like is “why spend 2 hours working something out when you can spend 7 hours automating it?”) etc. So get the baseline down and working and then streamline the code. Always think, “can this be done with fewer lines of code/quicker?” When I learnt about def functions I was gobsmacked.

What’s that thing people say, Rome wasn’t built in a day or something to that extent. But the more attention and time you take to studying and finessing your code, the better your future projects will be!

1

u/Vidiobeil Nov 18 '21

355 is still a lot but the improvment is there great job man

1

u/Smyles9 Nov 18 '21

One thing I noticed almost immediately in comp() is that you can use a loop and then use i-1 as the expression part nested inside the if statement

1

u/Smyles9 Nov 18 '21

Good job! Looks awesome!

1

u/womper9000 Nov 18 '21

I did this one awhile back https://pastebin.com/yExsv3XG just as an exercise for myself, I guess tell me if it's good or bad or maybe you can use it as an example, who knows. I'm SURE there are better ones than mine too but it's a learning process. There is always more than one way to do it in programming. (I usually do it the hard way)

1

u/Jeklah Nov 18 '21

I would like to see the original version to compare this to.

Good job on the refactor.

A good rule to follow is if you're repeating lines of code, there's probably a better way to do it.

1

u/gadabout___ Nov 18 '21

I won the game in this combination 1 8 7 4

1

u/[deleted] Nov 18 '21

Nice concept. At first I was wondering that why there is not GUI but nice work done. Impressed. I have followed you pls follow me back on git hub

1

u/RevRagnarok Nov 18 '21

Looks neat; I hadn't seen the original.

Starting at line 31, divmod might be your friend... it's great for mapping from 1D to 2D.

Don't have time to look more, but learning is an iterative process! ;)

1

u/StratInTheHat Nov 18 '21

Nice! But.. I won, with 5, 7, 1, 9.

1

u/flamestamed Nov 18 '21

… how did you even utilize 3642 lines?!

1

u/laundmo Nov 18 '21

functions!

glorious things they are, allowing you to write part of the code once and re-use it. Here's a step by step of refactoring (fancy term for "change it so it still does the same thing") code into functions. This is the absolute first approach, and after getting some experience you likely won't need it any more as you start writing code this way from the start.

  1. look for any repeated patterns in the code, like a similar if with a similar result and condition

  2. compare the multiple occurences. which parts are the same, and which are different?

  3. if the different parts are all some sort of value, either a variable or a "hardcoded" value like a number, go ahead. if theres differences in the functions called or keywords used, return to 1.

  4. copy one of those blocks into its own function.

  5. The values or variables that change are your function arguments. Give them each a good name. now, use that name as the argument passed into the function as well as in the places where its used in the function.

  6. use return for giving back a value, this could be anything. beware that return stops the function on that exact line.

heres a toy example:

x = int(input("amount: "))
v = ""
if x == 1:
    for i in range(1):
        v += i
elif x == 2:
    for i in range(2):
        v += i
elif x == 3:
    for i in range(3):
        v += i

steps 1-3 that if/elif with the loops looks awfully similar to me. each of them compares x with a number, and then loops that many times and prints out the loop index.

4.

def my_function():
    if x == 1:
        for i in range(1):
            v += i

5. so i have x and 1 to worry about. lets name x: compare and 1: comp_with and put them in their places.

def my_function(compare, comp_with):
    if compare == comp_with:
        for i in range(compare):
            v += i

6. Now the only thing left is that v += i. of course v doesnt exist inside the function yet, so we have to make it, and then also give it back at the end. Lets also rename it to not confuse ourselves.

def my_function(compare, comp_with):
    result = ""
    if compare == comp_with:
        for i in range(compare):
            result += i
    return result

now, thats our function done, but how does this look in the original code?

x = int(input("amount: "))
v = ""
v += my_function(x, 1)
v += my_function(x, 2)
v += my_function(x, 3)

thats looking much cleaner already. of course, a lot can still be improved about this example, but at least with just this function improvement i only have to repeat myself for 3 lines, instead of a lot more. there is one thing to consider with this though: we dont have the "only one can match" part of a long elif chain anymore. if somehow x managed to both be equal to 1 and 2, you would get more results than expected.

1

u/data_snob Nov 18 '21

Here’s a challenge, a long ass one liner.

1

u/_E8_ Nov 18 '21

... does that include AI?

1

u/CookieKola Nov 24 '21

Tic tac i’m gonna lick ur toe

1

u/atsbt Nov 30 '21

Wow! I'm in the middle of my first coding class and I'm dying! Our big project is due next week (a text-based game) and I'm having trouble just getting it to call a simple function.

1

u/MixPsychological2325 Dec 08 '21

I made one too. In vanillaJs as a online game. <~4000 columns

1

u/SandwichMayhem64 Dec 09 '21

Found another combo to beat the AI btw (1,6,8,9,3) u/M0pps

1

u/Educational_Ad8702 Oct 20 '23

How do I run this

0

u/[deleted] Nov 17 '21

[deleted]

31

u/[deleted] Nov 18 '21

[removed] — view removed comment

9

u/arkie87 Nov 18 '21

yeah, i never understand people who care about lines of code only. It should be readable and understandable.

27

u/scykei Nov 18 '21

Code golfing is kinda like a sport. There are a bunch of sites where you can golf with all sorts of languages.

You might not be able to appreciate it by just browsing through, but if you’ve attempted one of the problems, going through other people’s solutions and seeing how they do things differently from you is actually really fun. You’ll also learn a lot of neat tricks in the language (that may or may not actually be useful).

4

u/Bronigiri Nov 18 '21

Do you have a recommendation where I can python code golf?

2

u/scykei Nov 18 '21

I did a quick search but website I used more than five years ago might not be online any more… I’m sure there are tons of new ones now if you searched around. There’s also a code golf stack exchange, which I haven’t been frequenting, but if you browsed the hot posts, you’ll probably find lots of interesting problems and solutions for all sorts of languages including Python.

1

u/Bronigiri Nov 18 '21

Thanks I'll check it out

1

u/justingolden21 Nov 18 '21

Check the code golf stack exchange page and browse some posts. If you want to solve your own there are a few sites online with walkthroughs I bet, but just figured I'd mention the stack exchange.

9

u/PhantomlelsIII Nov 18 '21

Because its impressive. Not anyone can make something like that

3

u/arkie87 Nov 18 '21

It’s only impressive if it uses some unusual algorithm or clever way of reducing the number of lines. Even so, no programmer should ever do that in real code.

1

u/[deleted] Nov 18 '21

[removed] — view removed comment

3

u/CnidariaScyphozoa Nov 18 '21

They don't accomplish the same thing though. ;)

1

u/[deleted] Nov 18 '21

[removed] — view removed comment

0

u/irrelevantPseudonym Nov 18 '21

But then you'd have to play again before you'd played the first time.

if not play_again(): break at the end of the loop would be more succinct.

1

u/[deleted] Nov 18 '21 edited Nov 18 '21

[removed] — view removed comment

2

u/a8ksh4 Nov 18 '21

If you put a double space at the end of each line, reddit will preserve you newline character. fwiw.

-18

u/[deleted] Nov 18 '21

[removed] — view removed comment

10

u/sleep-enjoyer Nov 18 '21

Ah, programmers and their neverending struggle to out-asshole each other. Never gets old