{"id":4808,"date":"2022-08-24T15:13:42","date_gmt":"2022-08-24T09:43:42","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/"},"modified":"2022-08-24T15:13:42","modified_gmt":"2022-08-24T09:43:42","slug":"solved-java-tictactoe-dont-know-how-to-continue-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/","title":{"rendered":"[Solved] Java tictactoe. Don&#8217;t know how to continue [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-32678151\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"32678151\" data-parentid=\"32677756\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>I&#8217;ll help with understanding the logic behind such game and give you the testing code but advice you to do it on your own.<\/p>\n<p>First, you&#8217;ll need a flag to tell you if the game is finished.<\/p>\n<p>When does the game finish? 2 possible answers :<\/p>\n<ul>\n<li>One of the players wins<\/li>\n<li>The cells are all full without any winner<\/li>\n<\/ul>\n<p>That will be what you&#8217;ll be testing in your <code>do{}while()<\/code> loop<\/p>\n<p>After that, you&#8217;ll need to fill an array each time a player makes a move.<\/p>\n<p>You&#8217;ll have to be attentive to two things here :<\/p>\n<ul>\n<li>Number entered is an index of the array<\/li>\n<li>The number entered has not been played before<\/li>\n<\/ul>\n<p>First condition is easy but for the second you&#8217;ll need a way to &#8220;stock&#8221; the plays already made by both players.<\/p>\n<p>At the end of your loop, you&#8217;ll have to check if the game is finished and change your boolean if so.<\/p>\n<p>How does someone win tic tac toe? By having an horizontal, vertical or diagonal set of the same character. You&#8217;ll have to test all the possibilities and change your flag if filled.<\/p>\n<p>Otherwise, if your board is entirely filled, you&#8217;ll have to check that and break from the loop.<\/p>\n<pre><code>import java.util.ArrayList;\nimport java.util.Scanner;\n\npublic class TicTacToe{\n\n    private static Scanner scan = new Scanner(System.in);\n\n    public static void main (String[] args){\n\n        boolean finished = false;\n        int plays = 0;\n        int position = 0;\n        ArrayList&lt;Integer&gt; alreadyPlayed = new ArrayList&lt;&gt;();\n        String[] board = {\"_\",\"_\",\"_\",\"_\",\"_\",\"_\",\"_\",\"_\",\"_\"};\n        String character;\n\n        do{\n            character = plays % 2 == 0 ? \"x\" : \"o\";\n            do{\n            System.out.println(\"Player \" + character + \", it's your turn. (0-8)\");\n            System.out.println(\"Already played :\" + alreadyPlayed.toString());\n            position = scan.nextInt();\n            } while (position &gt;= 9 || alreadyPlayed.contains(position));\n            alreadyPlayed.add(position);\n            board[position] = character;\n            if (alreadyPlayed.size()&gt;=5){\n                finished = checkFinished(board);\n            }\n            plays++;\n\n            for (int i = 0 ; i &lt; 9 ; i++){\n                System.out.print(board[i]);\n                if ((i+1) % 3 == 0) System.out.println();\n            }\n\n        } while (!finished &amp;&amp; plays &lt; 9);\n        if (checkFinished(board)) System.out.println(\"Players \" + character + \" wins !\");\n        else System.out.println(\"No one wins.\");\n\n\n    }\n\n    private static boolean checkFinished(String[] toCheck) {\n        \/\/ Horizontal checks\n        if (!toCheck[0].equals(\"_\") &amp;&amp; toCheck[0].equals(toCheck[1]) &amp;&amp; toCheck[0].equals(toCheck[2])) return true;\n        if (!toCheck[3].equals(\"_\") &amp;&amp; toCheck[3].equals(toCheck[4]) &amp;&amp; toCheck[3].equals(toCheck[5])) return true;\n        if (!toCheck[6].equals(\"_\") &amp;&amp; toCheck[6].equals(toCheck[7]) &amp;&amp; toCheck[6].equals(toCheck[8])) return true;\n\n        \/\/ Vertical checks\n        if (!toCheck[0].equals(\"_\") &amp;&amp; toCheck[0].equals(toCheck[3]) &amp;&amp; toCheck[0].equals(toCheck[6])) return true;\n        if (!toCheck[1].equals(\"_\") &amp;&amp; toCheck[1].equals(toCheck[4]) &amp;&amp; toCheck[1].equals(toCheck[7])) return true;\n        if (!toCheck[2].equals(\"_\") &amp;&amp; toCheck[2].equals(toCheck[5]) &amp;&amp; toCheck[2].equals(toCheck[8])) return true;\n\n        \/\/ Diagonal checks\n        if (!toCheck[0].equals(\"_\") &amp;&amp; toCheck[0].equals(toCheck[4]) &amp;&amp; toCheck[0].equals(toCheck[8])) return true;\n        if (!toCheck[2].equals(\"_\") &amp;&amp; toCheck[2].equals(toCheck[4]) &amp;&amp; toCheck[2].equals(toCheck[6])) return true;\n        return false;\n    }\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Java tictactoe. Don&#8217;t know how to continue [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I&#8217;ll help with understanding the logic behind such game and give you the testing code but advice you to do it on your own. First, you&#8217;ll need a flag to tell you if the game is finished. When does the game finish? 2 possible answers : One of the players wins The cells are &#8230; <a title=\"[Solved] Java tictactoe. Don&#8217;t know how to continue [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\" aria-label=\"More on [Solved] Java tictactoe. Don&#8217;t know how to continue [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[323],"class_list":["post-4808","post","type-post","status-publish","format-standard","hentry","category-solved","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Java tictactoe. Don&#039;t know how to continue [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Java tictactoe. Don&#039;t know how to continue [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I&#8217;ll help with understanding the logic behind such game and give you the testing code but advice you to do it on your own. First, you&#8217;ll need a flag to tell you if the game is finished. When does the game finish? 2 possible answers : One of the players wins The cells are ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-24T09:43:42+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Java tictactoe. Don&#8217;t know how to continue [closed]\",\"datePublished\":\"2022-08-24T09:43:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\"},\"wordCount\":229,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"java\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\",\"name\":\"[Solved] Java tictactoe. Don't know how to continue [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-08-24T09:43:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Java tictactoe. Don&#8217;t know how to continue [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Java tictactoe. Don't know how to continue [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Java tictactoe. Don't know how to continue [closed] - JassWeb","og_description":"[ad_1] I&#8217;ll help with understanding the logic behind such game and give you the testing code but advice you to do it on your own. First, you&#8217;ll need a flag to tell you if the game is finished. When does the game finish? 2 possible answers : One of the players wins The cells are ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/","og_site_name":"JassWeb","article_published_time":"2022-08-24T09:43:42+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Java tictactoe. Don&#8217;t know how to continue [closed]","datePublished":"2022-08-24T09:43:42+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/"},"wordCount":229,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["java"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/","name":"[Solved] Java tictactoe. Don't know how to continue [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-08-24T09:43:42+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-java-tictactoe-dont-know-how-to-continue-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Java tictactoe. Don&#8217;t know how to continue [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=4808"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/4808\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=4808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=4808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=4808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}