{"id":1408,"date":"2023-06-18T20:04:50","date_gmt":"2023-06-18T20:04:50","guid":{"rendered":"http:\/\/tiemensfamily.com\/timoncs\/?p=1408"},"modified":"2023-06-18T20:04:50","modified_gmt":"2023-06-18T20:04:50","slug":"design-problem-from-tic-tac-toe","status":"publish","type":"post","link":"https:\/\/tiemensfamily.com\/timoncs\/2023\/06\/18\/design-problem-from-tic-tac-toe\/","title":{"rendered":"Design Problem from Tic Tac Toe"},"content":{"rendered":"\n<p>It is fascinating how quickly a simple problem can escalate to a very difficult design problem.<\/p>\n\n\n\n<p>Here are the major pieces needed to frame one of these difficult problems in Tic Tac Toe:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CellValue :   X, O, EMPTY\n\nBoard     :   state -> CellValue[9]\n\nGame      :   board -> Board<\/pre>\n\n\n\n<p>Everything is going well until we get to Game.getWinner().  In Tic Tac Toe, the winner is one of four things:  X, O, Draw or &#8220;unknown\/in progress&#8221;.   The above definitions already includes &#8220;X&#8221; and &#8220;O&#8221; &#8211; so, can getWinner() return type CellValue?  That is a bit awkward, since &#8220;EMPTY&#8221; makes sense for a Board, but &#8220;EMPTY&#8221; is a poor name to use for &#8220;Draw&#8221;.  And what string should &#8220;EMPTY&#8221; return?  The &#8220;-&#8221; to use when printing a board, or &#8220;Draw&#8221; for printing the winner?<\/p>\n\n\n\n<p>One possible solution is this design:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Player    :   X, O\n\nCellValue :   X(Player.X), O(Player.O), EMPTY\n\nWinner    :   X(Player.X), O(Player.O), DRAW, INPROGRESS\n\nBoard     :   state -> CellValue[9]\n\nGame      :   board -> Board<\/pre>\n\n\n\n<p>Now, Game.getWinner() returns type Winner, and there are no &#8220;null&#8221; values floating around to spawn runtime errors.  The two similar &#8220;Player&#8221; concepts are captured explicitly (i.e. &#8220;X&#8221; can be both a value on a Board, and a winner of a Game), with only a small glitch around things like &#8220;Does board.state[3] equal game.getWinner()&#8221;?  i.e. the sub-problem of comparing a CellValue with a Winner, when they currently share no useful common parent type.<\/p>\n\n\n\n<p>If your goal is to get a good grade in a software engineering class, then use the second design.  If your goal is a software project that can generate Kaggle CSV dataset files, the first design will suffice (See <a href=\"https:\/\/github.com\/timtiemens\/tictactoe\">tictactoe<\/a>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is fascinating how quickly a simple problem can escalate to a very difficult design problem. Here are the major pieces needed to frame one of these difficult problems in Tic Tac Toe: CellValue : X, O, EMPTY Board : &hellip; <a href=\"https:\/\/tiemensfamily.com\/timoncs\/2023\/06\/18\/design-problem-from-tic-tac-toe\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,6],"tags":[],"_links":{"self":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1408"}],"collection":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/comments?post=1408"}],"version-history":[{"count":1,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1408\/revisions"}],"predecessor-version":[{"id":1409,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/posts\/1408\/revisions\/1409"}],"wp:attachment":[{"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/categories?post=1408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiemensfamily.com\/timoncs\/wp-json\/wp\/v2\/tags?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}