[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-672925-105":59,"doc-detail-672925-en":131,"doc-similar-672925":161},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":124,"head_meta":126,"extra_data":128,"updated_unix":130},105,"en","gcse-computer-science-creating-battleship-using-arrays-to-create-a-game-of-battleship","GCSE Computer Science - Creating Battleship - using arrays to create a game of Battleship","","Guided GCSE Computer Science activity explaining how to build a Battleship-style game using a two-dimensional grid represented with arrays. The document specifies a step-by-step pseudocode flow for placing a ship, capturing user coordinates, updating grid cells based on '.', '*', and '4', and checking whether the entire ship is sunk. It then expands on implementation details for declaring and initializing arrays, adding ships using direct assignment and loops, and detecting hits via conditional logic, with working JavaScript code provided.",{"@graph":69,"@context":123},[70,84,106],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":20,"@type":76,"position":81},"https://docshare.wps.com/document/exam/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/gcse-computer-science-creating-battleship-using-arrays-to-create-a-game-of-battleship/672925/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/gcse-computer-science-creating-battleship-using-arrays-to-create-a-game-of-battleship/672925.png","ImageObject",300,407,{"name":92,"@type":93},"WPS_1790064749","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-12-02","2026-11-02",true,{"@type":102,"interactionType":103,"userInteractionCount":105},"InteractionCounter",{"@type":104},"ViewAction",11,{"@type":107,"mainEntity":108},"FAQPage",[109,115,119],{"name":110,"@type":111,"acceptedAnswer":112},"How does the document represent the Battleship board?","Question",{"text":113,"@type":114},"It represents the board as a 9 by 9 two-dimensional array called 'game', where each grid point stores characters such as '.' for empty, '*' for hit, and '4' for the placed boat segment.","Answer",{"name":116,"@type":111,"acceptedAnswer":117},"What inputs are used to place the boat in the pseudocode?",{"text":118,"@type":114},"The user inputs the starting column (stored as 'x'), the starting row (stored as 'y'), and whether the boat is horizontal (h) or vertical (v).",{"name":120,"@type":111,"acceptedAnswer":121},"How is an attack evaluated and how does the board update?",{"text":122,"@type":114},"The second user enters x and y coordinates. If the selected cell contains '.', it outputs 'miss'; if it contains '*', it outputs 'boat already hit'; if it contains '4', it outputs 'hit' and changes the cell value to '*', followed by a check for whether the whole boat has been sunk.","https://schema.org",{"og:url":83,"og:type":125,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":127,"canonical":83},"index,follow",{"doc_id":129,"site_id":62},672925,1793613355,{"code":4,"msg":5,"data":132},{"doc_id":129,"user_id":133,"nickname":92,"user_avatar":134,"doc_module":4,"category_id":19,"category_name":20,"doc_title":65,"doc_description":67,"doc_content":135,"file_id":136,"file_url":137,"file_type":138,"file_size":139,"view_count":105,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":39,"language":140,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":141,"faqs":142,"seo_title":143,"seo_description":67,"update_tm":130,"read_time":46,"preview_page_count":19,"preview_urls":144},3985747859154,"https://ap-avatar.wpscdn.com/davatar_276721f389ce27ea32af1340a28f341c","GCSE Computer Science  \nCreating Battleship: using arrays to create a game of Battleship  \nIn the following example, we will look at how a two-dimensional array can be used to represent Battleship , the board game by Hasbro.  \nBattleship is a game which is played on a two-dimensional grid. Each point on the grid represents a space on a map of the sea where a ship might be. Two players plot their ships on a grid in secret and then take it in turns to guess where their opponent has plotted their ships.  \nThis activity includes pseudocode, followed by explanations about how to code each step in JavaScript, followed by a complete coded solution in JavaScript and Python 3.  \nNOTE: in our example we will only show how one player can place a ship and how another can try to sink it.  \nPseudocode  \nWe are going to create a game which follows this pseudocode:  \n1. create a 9 by 9 array called ‘game’  \n2. ask the user to input the column where their boat starts – store this value as‘x’  \n3. ask the user to input the row where their boat starts – store this value as ‘y’  \n4. ask the user to say if they want the boat to be horizontal (h) or vertical (v)  \n5. convert all inputs from character strings to numbers  \n6. the boat that is created is called ‘4’  \n7. set the length of the boat to be 4  \n8. if h is selected:  \n􀁸 counting from x+0 to x+3 using ‘c ’, put a ‘4’ on the game grid at [y][c]  \n9. if v is selected:  \n􀁸 counting from y+0 to y+3 using ‘c’, put a ‘4’ on the game grid at [c][x]  \n10. display the result  \n11. the second user attempts to locate the boat by entering ‘x’ and ‘y’ coordinates  \na. if a ‘.’ Is found at the selected coordinates then a ‘miss’ is output  \nb. otherwise ifa ‘*’ is found then a ‘boat already hit’ is output  \nc. otherwise if a ‘4’ is found then a ‘hit’ is output and the cell value is changed to ‘*’  \n12.check to see if the whole boat has been sunk:  \na. if yes then game over  \nb. otherwise continue  \nThe solution  \nA. Creating the grid  \nThe following code examples should work if copy and pasted into an html document and executed in a web browser:  \nTo declare this array in JavaScript , we could use the code:  \nchar game [9][9];  \nWe can declare and initialise an array as follows-by giving it some values when we create it. For example:  \nchar game [9][9] = {  \n{'.','.','.','.','.','.','.','.','.'},  \n{'.','.','.','.','.','.','.','.','.'},  \n{'.','.','.','.','.','.','.','.','.'},  \n{' .',' .',' .',' .',' .',' .',' .',' .',' .'},  \n{'.','.','.','.','.','.','.','.','.'},  \n{'.','.','.','.','.','.','.','.','.'},  \n{'.','.','.','.','.','.','.','.','.'},  \n{'.','.','.','.','.','.','.','.','.'},  \n{'.','.','.','.','.','.','.','.','.'},  \n};  \nThis would fill every row and every column with a full stop.  \nB. Adding ships  \nThe above code establishes the grid but what if we want to add some ships? The following example shows the grid with a ship called 1 and another ship called 5.  \n\n|  | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |\n| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n| 0 | 1 | . | . | . | . | . | . | . | . |\n| 1 | . | . | . | . | . | . | . | . | . |\n| 2 | . | . | . | . | 5 | 5 | 5 | 5 | 5 |\n| 3 | . | . | . | . | . | . | . | . | . |\n| 4 | . | . | . | . | . | . | . | . | . |\n| 5 | . | . | . | . | . | . | . | . | . |\n| 6 | . | . | . | . | . | . | . | . | . |\n| 7 | . | . | . | . | . | . | . | . | . |\n| 8 | . | . | . | . | . | . | . | . | . |\n\nThe one in the top left of the grid represents a ship which is just the size of one grid space. It would be created with the following line of code:  \ngame [0][0] = '1';  \nThis saves a '1' in the array at the position referenced by the index [0][0] .  \nThe 5s represent a bigger ship. Bigger ships (several characters in one go) can be created by manually assigning each character or by using a FOR loop (FOR loops are explained in more detail in the Algorithms and control flow Study guide) . The following code creates a ship called ‘5’ which is five data points long. ","cbCaisBJ0N9aWkhT","https://ap.wps.com/l/cbCaisBJ0N9aWkhT","pdf",440514,"English","# Pseudocode\n## Creating a 9 by 9 game array\n## Placing the ship horizontally or vertically\n## Processing user attacks and updating grid cells\n## Checking whether the boat is sunk\n# The solution\n## A. Creating the grid\n## B. Adding ships\n## C. Finding the enemy ships\n# The full JavaScript code","[{\"question\":\"How does the document represent the Battleship board?\",\"answer\":\"It represents the board as a 9 by 9 two-dimensional array called 'game', where each grid point stores characters such as '.' for empty, '*' for hit, and '4' for the placed boat segment.\"},{\"question\":\"What inputs are used to place the boat in the pseudocode?\",\"answer\":\"The user inputs the starting column (stored as 'x'), the starting row (stored as 'y'), and whether the boat is horizontal (h) or vertical (v).\"},{\"question\":\"How is an attack evaluated and how does the board update?\",\"answer\":\"The second user enters x and y coordinates. If the selected cell contains '.', it outputs 'miss'; if it contains '*', it outputs 'boat already hit'; if it contains '4', it outputs 'hit' and changes the cell value to '*', followed by a check for whether the whole boat has been sunk.\"}]","GCSE Computer Science - Creating Battleship - using arrays to create a game of Battleship | PDF",[145,149,153,157],{"page_no":8,"url":146,"width":147,"height":148},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-18/644453587cb804751d52b96ae5f63e8d.webp",983,1390,{"page_no":14,"url":150,"width":151,"height":152},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-18/1bd8f69a120048b7cf28750979b73ee3.webp",982,1388,{"page_no":81,"url":154,"width":155,"height":156},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-18/45052b6cd3f1fec4b92cc448ec185b03.webp",981,1387,{"page_no":19,"url":158,"width":159,"height":160},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-18/d57361623d80fd61b3bb8605ee3e65c8.webp",980,1385,{"list":162,"token":66},[163,170,175,182,188,194,200,206,212,219],{"doc_id":164,"doc_title":165,"file_type":138,"home_preview_img_url":166,"slug":167,"view_count":168,"category_name":20,"create_tm":169},671920,"Multiplication Battleship - read online free","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229988802826/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=daKjArmZZ04IVoIxlGpdx1qIeUw%3D","multiplication-battleship-read-online-free",13,1793582449,{"doc_id":171,"doc_title":172,"file_type":138,"home_preview_img_url":173,"slug":174,"view_count":44,"category_name":20,"create_tm":169},671921,"How to Play Battleship - Printable Game Instructions","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229988424231/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=Fgft4t5DVEJzj8NGDClnjUo46ug%3D","how-to-play-battleship-printable-game-instructions",{"doc_id":176,"doc_title":177,"file_type":138,"home_preview_img_url":178,"slug":179,"view_count":180,"category_name":20,"create_tm":181},672919,"Battleship - Years 5-6, 7+ - practise coordinates","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229990189123/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=p2t6E9ONZwie%2B9D%2FAEsj%2FnQk%2F8M%3D","battleship-years-5-6-7-practise-coordinates",16,1793613339,{"doc_id":183,"doc_title":184,"file_type":138,"home_preview_img_url":185,"slug":186,"view_count":39,"category_name":20,"create_tm":187},672920,"Coordinate Graphing Battleship","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229990189106/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=HatYZ%2FQukJA9IqNPpVHdXMbQVZk%3D","coordinate-graphing-battleship",1793613341,{"doc_id":189,"doc_title":190,"file_type":138,"home_preview_img_url":191,"slug":192,"view_count":29,"category_name":20,"create_tm":193},672918,"Coordinate Grid Battleship - Educational Game Worksheet","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229989900068/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=Aqw0fNrMKkRcNTcx1%2FRAJ712k5k%3D","coordinate-grid-battleship-educational-game-worksheet",1793613338,{"doc_id":195,"doc_title":196,"file_type":138,"home_preview_img_url":197,"slug":198,"view_count":29,"category_name":20,"create_tm":199},478893,"Multiplying Using Arrays - Answer Key","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229926735357/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=1ad7ukP6AmXVLD%2BfBQ41RdkZvek%3D","multiplying-using-arrays-answer-key-478893",1790792368,{"doc_id":201,"doc_title":202,"file_type":138,"home_preview_img_url":203,"slug":204,"view_count":34,"category_name":20,"create_tm":205},476148,"Using Grids in Real World Context - Answer Key - Lesson Problems","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229926697113/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=F2iCaBh%2B1UJWFqkyZ3gsCefI0ZA%3D","using-grids-in-real-world-context-answer-key-lesson-problems",1790787337,{"doc_id":207,"doc_title":208,"file_type":138,"home_preview_img_url":209,"slug":210,"view_count":44,"category_name":20,"create_tm":211},476150,"Using Grids in Real World Context - Solve worksheet problems","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229926697153/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=F%2FJCpULYAiQzg4B6LGs3aat1LI4%3D","using-grids-in-real-world-context-solve-worksheet-problems",1790787341,{"doc_id":213,"doc_title":214,"file_type":138,"home_preview_img_url":215,"slug":216,"view_count":217,"category_name":20,"create_tm":218},472832,"Vertical Division with a Helper Grid - Solve each problem","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229926375556/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=TsYTumBYTsplo%2B2p1cQy5VjXD00%3D","vertical-division-with-a-helper-grid-solve-each-problem-472832",14,1790781477,{"doc_id":220,"doc_title":221,"file_type":138,"home_preview_img_url":222,"slug":223,"view_count":39,"category_name":20,"create_tm":224},493575,"Multiplication - Count in equal groups of 2s, 5s, and 10s","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110229928780508/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796231339&Signature=dOJNveddka2AprdYoWB%2Fd9iJveQ%3D","multiplication-count-in-equal-groups-of-2s-5s-and-10s",1790884182]