From c01ac5a8c53961347668e6db9e5ba536570d64b6 Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Mon, 11 Mar 2024 14:10:21 -0600 Subject: [PATCH] implemented basic challenge structure. --- spring24/week4/Challenge$Stack$Node.class | Bin 0 -> 764 bytes spring24/week4/Challenge$Stack.class | Bin 0 -> 1400 bytes spring24/week7/Challenge$Stack$Node.class | Bin 0 -> 651 bytes spring24/week7/Challenge$Stack.class | Bin 0 -> 2216 bytes spring24/week7/Challenge.class | Bin 0 -> 6404 bytes spring24/week9/Cell$List$Value.class | Bin 0 -> 722 bytes spring24/week9/Cell$List.class | Bin 0 -> 2155 bytes spring24/week9/Cell.class | Bin 0 -> 2489 bytes spring24/week9/Cell.java | 285 ++++++++++++++++++++++ spring24/week9/Sudoku.class | Bin 0 -> 4695 bytes spring24/week9/Sudoku.java | 225 +++++++++++++++++ 11 files changed, 510 insertions(+) create mode 100644 spring24/week4/Challenge$Stack$Node.class create mode 100644 spring24/week4/Challenge$Stack.class create mode 100644 spring24/week7/Challenge$Stack$Node.class create mode 100644 spring24/week7/Challenge$Stack.class create mode 100644 spring24/week7/Challenge.class create mode 100644 spring24/week9/Cell$List$Value.class create mode 100644 spring24/week9/Cell$List.class create mode 100644 spring24/week9/Cell.class create mode 100644 spring24/week9/Cell.java create mode 100644 spring24/week9/Sudoku.class create mode 100644 spring24/week9/Sudoku.java diff --git a/spring24/week4/Challenge$Stack$Node.class b/spring24/week4/Challenge$Stack$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..908f7bad2c07629ca22a489e0af13c79edeab947 GIT binary patch literal 764 zcma))U279T6o%hP(`>rNBu!hbAFX3dp}lFS#2Xbsf}#-OMQXvz$>xx3JDFK#C$axb zP|$mSlz3*bREmAaBhgu}+n*_|$xNv; z@+vv&4DEy>lm(inYN@(fnQ8ak_>>Yau)0)wK?1G8<=x1K=bj0a4V^QyH~5baFkbPq zH18Y<$ZHA$`#d$Ne|2p{Z&JIqvw|+5IL!fcl`77tFt72WizBi6?tCuv9iJfPu zRz9^xX3mZ^&7aD6?RZ9VLWx$6nT&-}QSj> z9)@WJYHy7p*Vih~DUWbpVE3Y>tG6J+uE6%c7+!>8S6J7uEdtdc<^GM$JtPEHc-vOk zucE?{K!hsLY+?;@kqaKY#^*OV>oFPl_zUVETFp;redDZzEw&YALj5hafoBT0!&_Xj a&9;O)xXZr9(LLKh@B zYSnt5JC1OBqSV&hK1w0MkUQijyy|ebSKaFz3R^QI`hs^Ewwm+HOD)wEJCv@ubjSqq zy7^;3p5Yj#@sfRO8SQ3^SR?1b#D?<+rzJcD}a`XW%y ziRhZP8hFy-S}ND{eARKp;IU~e?)Qb+6}H2D%JHam-VXSk*ZiMN^1d*K|hh`z&K=?HKIPcQv4H>kQ-W_=Q9fhp& zYdSsUF)Ul#BqnzkKryXvU&0t%D7MV0eKa&)#3N@Z%O14)c^nh literal 0 HcmV?d00001 diff --git a/spring24/week7/Challenge$Stack$Node.class b/spring24/week7/Challenge$Stack$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..8a0fe3f3544b8e5b4f8e54b38d1c0c7fab5851c3 GIT binary patch literal 651 zcma)3T}uK{5Iy79s%>gkW*?fBfm$R*(L)i05Jh4ShVNIq=<2q{?rQy+>NoTtK@a_a zepEDfiF$~#mpdQl%-lIMZ|^U!0FJS$!ADqgZfwJ9@9dUurR?pOD;;5)(rU|m-4(4~ zzSh5z8UlnwR}4kj6jr-@(d^2WO9&2yIgo^8^&6|Zs@(x0V97h?tXKc>0nW&PF8I2Y!y}&0U@pfT;{^XXe$(0Rv$)sa2PONPTtCKol{7w!G!Y*;;u~ zR_Dw`n;7HyO3}3E&8nro{mqa9Es)$3OIUP)GNC#5aR((&9>GsbW(g0li0 zn*ZZz42&Z!5K(R6*-jJ}XxL#z_>zGE9Ho5vh#|#VwaScSS*X;iMR~iMH$m?9Yl#bgChd4Q`_Qd!3?(V}gl6`Z1j6)eGq0 zuS7R4;Ti54OvfbX#UakBK%ZYg@|FUY+Ai=j`WLc850GjU3hh$pG7C+z=oJ>YN`6zi zP(q_k`GuHPiOxN0foJ`aInD}BIJ<%Fdt3yd@Hgqr=^c2(4ZO#Y_AmcV2^HR=Mmb27 zGfdODg^uiy!n?qnp<45GISsBu@fo6 z5NpH`c!*R4{VDQ2{(oS-M1+#8ZxJHd8VBqycJ#odvwh)_#wc zc^`3nfG&JU$v)yEe@r}|U=*L?3_fG?d`?S!p$XL6X3i(jg>%{x;*lld4{?m1Ae9NW zbzK~nbQGap4gWjD$w+}ACr+9C;MQM%s2_*AT0Q!==1VsFU3TJE1okz7-DUA_b^z%0 o0fc#WSvO?ElTp@hWwJ@846y01a*lH)*!VATR^X;N=k@v2-(XRw3IG5A literal 0 HcmV?d00001 diff --git a/spring24/week7/Challenge.class b/spring24/week7/Challenge.class new file mode 100644 index 0000000000000000000000000000000000000000..0d3095f777ce326067bf116728ccab0b51050f7b GIT binary patch literal 6404 zcmbtY3wRt?b^g!pYG<@sSZoNMUY8n}GIKdA zKWS+Q2$ml*kC>^PnV(GUIB>|yID+7YY(DF36!@dD-Gab?Jz<65M{O822nrg8vw3T@ zJbl0_?lKSLEJ0-0&X~E~W-+V!mBD~Bl`RQY4PR>iet}-J%!#3_;&DxM*cByfr_?}y z%!%D#wE=AeN=2GJsGoM#;E6}8*zml`R zxX*4i+Y-hqynzDctrLWBqn+Zx7dTt!Ky&BNA!ozIxgo~$#)R>?8*4t2i1 zvPZS>OE6O+UmeCZxRypY3k54bAxK`<+KSTsO7`o*xE?nM>ZZ)n=)CwmyuE6JSQv3v zGspJqrIxgXkyJ-nlgZg79&QR98g3MfVbto_Ndp)m3}GycTQDxT;WhTFX06uW9>#9$VLTuN1dY`zy_nG- z+pB`W0(dH#GkD~vD%V`X0zt<=QXTiQ{uil*}1OKh-L$J;sm-W5wlStg?EQx>%~Cdw*vv#8@8f+TWGxB1tG`Pe!}fb@j#)ouO!VduLZ~yrZ|Xy*rld=xOij zTo*F)rTv}pt`LKkl6XGJb}(Ko4JOxVl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FL;E7x zE0N-7F-(h(jwB*GX7Q#`|Jnep)s}1wbk1Vep5p;&3?$Dzbm0X#Me2ZSgjMc|C8498 z=XfK{&_OkCL=;_B%EsrFjZ?8VQwp80Wo%2O9%&9V2Q!bU za?&jiyQS@xrN@>%Mmi5!ao4oZ)3h&8(ie%ZpI|3`%E#N!XsDN{_sf{Vt6KMiZjGZg#=P2-~jHoF+|9HtKbS8=Ti7avu4&%7f2Bvn??5|Q7ekV>j5ut;ka zShV@wv1-0A9dr!=C(%0)h~V(LtNoDY2OEMJ;^SbAyH%C z48E+wIM=%YhC%${hb}zj@)~03sqk_)TwoU?yQL;89KTY{Q3?Feh0_6b$X?KT0&P61 z7<7rFK@w<^HbO3mHPViRq|hlhqE|YwSvvWds0*XgjoYOMcgR{~WF6n{>-qNX#fTk*6E;cK!DUzc0(ZMhXM%58W_hVioO z5RskIEN_xt8IykYx5@1?BD-Z$_Q-qW&GKIMPslzOkQ-I7m6LOTWGn4PFI_%_r`%xr zIG)2fw){BHFz*Ia?zf{Vtf~m;(W|iP(cejW<@t@!U&V`VfE})Vg*mv_w``MgoTu?s zF9OgH`L&s(&T{=LTH3!7e>j#7xMj^s{LxrC*f5sXR^m^_()vm~GnQT+T!}wr#j!sh z8)-Y2I9Gwp*FDHEQmkof?MSqCeB=bKR3LFXt!MC!xihT9j_fD_L4ZEn7vNCjpJ(x}tP|>L@`Dr5BR_luHIe_M zsnlJ{)BPHTX~^9?#Cxds+o<>33EX=L+;{Tx?s06F`&@mzYsRhkFUCc1&CU2RGblgr z@pG-ZaQGN0JKc!pM*>&5Pzty(SO?z)KF?|xH|K5-)bn|DfjFh^6cRFIpi2JvNyVqN1SBXrTBMevb?k>Z9%NlB_5Tlb77>~N literal 0 HcmV?d00001 diff --git a/spring24/week9/Cell$List$Value.class b/spring24/week9/Cell$List$Value.class new file mode 100644 index 0000000000000000000000000000000000000000..0456d1b5158c34e20bf8aadc597267a18e50fba8 GIT binary patch literal 722 zcmaJ;O-lk%6g_v;nY5`)&C0&CEYct;+C&kA5D8+7!u@Cuo0`U$H(GzIg#<170sW}x z-YKF*wt4sK+>dkK`^W1WfKzN)FbJ#qA#nZP{z2_gsk_5kU3p%)>FTiDcD#YI5GSN> zouO0n9KTn)=-jGqNJtE&N}vWI?yDzG6`TK$IwF|ijjPLhgu+;?ppISN4Nv&C?eVZi zrOlh`{cDv3Va7%hsT5+EwK0hdA+}v zMZ&^3y)3K~vVT;Wv3w27AAJsCqNQ|r-q#_D%rM6P8iOH(*)xPLkWPyFNv8)#VYgRT^RLIYQ6#n?W3)ERvMNG|eGPc`R@~&)wXMMcEK}veV$% z1uT7UIN?}Orfm2%GbLGCM1l&xW|=>;%;;`mv&`KFRQ7*kDWSmDz4a_0lI> C#fotN literal 0 HcmV?d00001 diff --git a/spring24/week9/Cell$List.class b/spring24/week9/Cell$List.class new file mode 100644 index 0000000000000000000000000000000000000000..5b3d5f33d0ff32f290b3e7a182b3c778f6a9a43e GIT binary patch literal 2155 zcmah~U2Icj7=FI?w5Kg+V;e9=f3^wR+|IfVaDq6T;uM*7!vxca+;AvID3oq#J3tbH z7m|3RcixyN3`rJ}sbKhtV2E4@ivRycjqy^E7!z+4k@>veX$px|Z@$0t{ygvVKHqmP zT|aXUz#2Sk!l%$unkeQ9+m;XX?{VFotNMrCe7+-{E0s+c3Jp7)-A;erDQxRszh#G; zEh`w?U1zI8d-~rTJ2Fmw(jBB_*~#TOm2&%B1vR4J-{tI;F;`Fsmbd3h9ZMA!)QUv+ z;Obl7i-ONM0U>QwR%Jns{QWX*{@zh7p$BXCZ}t669=UAB}G9 zAF2AYDBmXbO63&lHk6(0&QWJV7j5Epg?TkVn^+bm*gGt&LXaT~?%uMXAGEL-u(g(Wqi={hR1k*0OxZVUHFGXlFsOA4WayQfl}zr1ULA+wXkqFZ1{vpl}0SahC{ zQhEeJX@VavkWb^|0JR^#ARqbu7kC6Y-JGOC5Ao7P=_!NQ)t0qB=9m8 z;|OlySBnW4mir}~C1pn?~YD?;U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oWY$oE58^24-=BIuSx) z53#oZX{YgpvY6gB@F{LFtI)sPXjp5c!mht1gCLud%t3n2Z#Js-RYzK-PbTQ3$U}-# zz8?l*!>Ui);j&)~()bKtB=NaIZgadG?}@)+7b1TtC>j?0g z<<(p}$W?rQrCBe&E_cVt7RTFbb-RoCLWg}c^~KIMXhD=L*PU}hySO`ng%lPQj_Lk1 zmFWbg=ZB_c@=|bW@@(4XL%(6lte91iCIxDq?{KhOHfC+je06_pvsW<6o@X~ot`!7! zki?S0=xgeA(<2E-VdVc!S;j~_Hi}$L3Oi=(Ae^>k`WcLEJo2WCC8v|X5%yLOr>d9j z#hZ$|nA}r(WvA+urLm1)T5&mvQa5##$_y!!-w3OZnv6v2@YlZ)YIC1`# z+ZO)NI!52jVT36H) zzM`ElGPf~m3v%KG#=0nSo?b=bO_I$8DsqvTU+Q3Dwwai1CT5$7$;G#JvoCQ5*uc5x`9CqTfs0bLQ7KRl#eY?xGHZREg52P@YKBy9 zGTB*8=RgG7rZcKVQ2Z!^Da3Xd3_pcZi$sB|6t9=TMt|cbrh_E(;av?V4$^>8#T7p4 z>U?Fpv(?r4<97N#3L64XNSfIVT>JS|`jsgCY;2n>)0?u;er8jWqr9{Mmy)bf5|5Jj z?2|e>@7#CF^L0mtsTOEV z{kFS24Xy94ykBQ0%X1mnL9G4< D{AAk( literal 0 HcmV?d00001 diff --git a/spring24/week9/Cell.java b/spring24/week9/Cell.java new file mode 100644 index 0000000..38a6087 --- /dev/null +++ b/spring24/week9/Cell.java @@ -0,0 +1,285 @@ +package spring24.week9; + +/** + * A Cell represents a single cell in the Sudoku grid. + * This helper class is used to store the row, column, value, and possible values for a cell. + * The possible values are stored in an ArrayList of Integers, and are sorted in ascending order. + * + * The Cell class also contains a method to add a possible value to the cell, and a method to get the possible values. + */ +public class Cell { + private int row; + private int col; + private int box; + private int value; + private List possibleValues; + + /** + * Create a new Cell with the given row and column. + * + * The value of the cell is initially -2, and the possible values are initially empty. + * + * @param row + * @param col + */ + public Cell(int row, int col) { + this.row = row; + this.col = col; + setBox(); + possibleValues = new List(); + } + + /** + * Create a new Cell with the given row, column, and value. + * + * The possible values are initially empty. + * + * @param row + * @param col + * @param value + */ + public Cell(int row, int col, int value) { + this.row = row; + this.col = col; + this.value = value; + setBox(); + possibleValues = new List(); + } + + /** + * Create a new Cell with the given row, column, and possible values. + * + * The value of the cell is initially -2. + * + * @param row + * @param col + * @param possibleValues + */ + public Cell(int row, int col, int[] possibleValues) { + this.row = row; + this.col = col; + setBox(); + this.possibleValues = new List(possibleValues); + } + + /** + * Set the value of a cell. + * + * The value must be between 1 and 9, inclusive. + * + * @param value + */ + public void setValue(int value) { + if(value < 1 || value > 9) { + System.out.println("Value " + value + " is not a valid value for this cell."); + return; + } + + possibleValues.clear(); + this.value = value; + } + + /** + * Get the value of a cell. + * + * @return + */ + public int getValue() { + return value; + } + + /** + * Get the box a cell is in. + * + * @return box number + */ + public int getBox() { + return box; + } + + /** + * Add a value to the list of possible values. + * + * If the value is already possible for the cell, or if the value is not a valid value for the cell, then the value is not added. + * + * @param value + */ + public void addPossibleValue(int value) { + if(possibleValues.contains(value)) { + System.out.println("Value " + value + " is already possible for this cell."); + return; + } else if(value < -1 || value > 9) { + System.out.println("Value " + value + " is not a valid value for this cell."); + return; + } + + possibleValues.add(value); + } + + /** + * Get a list of possible values for the cell. + * + * This list is a copy of the cell's possible values, and is sorted in ascending order. + * + * @return list of possible values for the cell + */ + public int[] getPossibleValues() { + return possibleValues.toArray(); + } + + /** + * Given the cell's row and column, set the box number for the cell. + */ + private void setBox() { + int boxRow = row % 3; + int boxCol = col % 3; + box = boxRow * 3 + boxCol; + } + + /** + * This class is used to store the possible values for a cell in a sorted list. + * The list is sorted in ascending order, and the values are stored in a linked list. + * + * The List class also contains a method to add a value to the list, and a method to get the list of values as an array. + */ + private class List { + + private Value head; + private Value tail; + private int size; + private int max; + private int min; + + /** + * Create a new empty List. + * + * By default, the head and tail are null, the size is 0, and the max and min are 0. + */ + public List() { + head = null; + tail = null; + size = 0; + max = 0; + min = 0; + } + + /** + * Create a new List with the given initial value. + * + * By default, the head and tail are the same, the size is 1, and the max and min are the initial value. + * @param initValue + */ + public List(int initValue) { + head = new Value(initValue); + tail = head; + size = 1; + max = initValue; + min = initValue; + } + + /** + * Create a new List with the given initial values. + * + * @param initValues + */ + public List(int[] initValues) { + for(int i = 0; i < initValues.length; i++) add(initValues[i]); + } + + /** + * Add a new value to the list. + * + * By default, the value will be inserted at it's sorted place in the list. + * + * @param newValue + */ + public void add(int newValue) { + if(size == 0) { + head = new Value(newValue); + tail = head; + size = 1; + max = newValue; + min = newValue; + } else { + if(newValue > max) { + max = newValue; + tail.next = new Value(newValue); + tail = tail.next; + } else if(newValue < min) { + min = newValue; + Value tmp = new Value(newValue); + tmp.setNext(head); + head = tmp; + } else { + Value current = head; + while(current.next != null && current.next.value < newValue) { + current = current.next; + } + Value tmp = new Value(newValue); + tmp.setNext(current.next); + current.setNext(tmp); + } + size++; + } + } + + /** + * Get the list of values as an array. + * + * @return array of values in the list + */ + public int[] toArray() { + int[] array = new int[size]; + Value current = head; + for(int i = 0; i < size; i++) { + array[i] = current.value; + current = current.next; + } + return array; + } + + /** + * Check if the list contains the given value. + * + * @param value + * @return + */ + public boolean contains(int value) { + Value current = head; + while(current != null) { + if(current.value == value) return true; + current = current.next; + } + return false; + } + + /** + * Clear the list of all values. + */ + public void clear() { + head = null; + tail = null; + size = 0; + max = 0; + min = 0; + } + + /** + * Value class is a Node for the List class. + */ + private class Value { + + private int value; + private Value next; + + public Value(int value) { + this.value = value; + next = null; + } + + public void setNext(Value next) { + this.next = next; + } + } + } +} \ No newline at end of file diff --git a/spring24/week9/Sudoku.class b/spring24/week9/Sudoku.class new file mode 100644 index 0000000000000000000000000000000000000000..7800c899391f0879177eb36ed81fedd60c846523 GIT binary patch literal 4695 zcmcIn?Q>Lj6@GrZclYitn-CHf2qbuEp-BjkmX~cwFbaV{Y(gTTix5RFyEn-tn|tHl zyUUBVwix?j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Ap{)uHkCx!(E4hYeRFr~XjZEN|E5XybN4%W|G*x~p-z zYCC5YmV>l{)D6nM-@3ojz@gsqa|JjtX_i4u_t{Y*lNx%wT zac{|^%DXO}1<#SKw-9@W7a=5gl&^tK8Usn+faf~i#Zixyd?G*o3tdKe&fG*p({>#WGed><4LE`Uk|g9_D9E*+HX(YcXAYgW@iBmT zil6d{&sf5Hdyq(vc(K>`)&Xn$4o4YBlFhwN;B%NhWl`#UpTnNdg|vN_$LQiI&!y%Y zS6tQwF6qDn9Ad~3d^~V|im508;Fi|gI9Ahpmfz+)?^}KAiuZcYr<6fG<7a|&PVg$s z2;AQVarX0#BqlD4`w5bAe*y*8&8gxfUI`HoLWFP%V|4OKx(VS^Qd?pbucXFC?%*_K za^|R#R_3rToQ{Ok(K)Q|Y}uKLQcHSDxsJG!j?AHMHY!rlhWL$pe@jKKp>BIhc^4D& zm_9M9h?LS0f9u}++hxTRPvgF-o+i3ynBwR8C*fJF#dFw#%T&l0sQzcEkLRhU7pRtJ zNcPKK7Ds$sl`KXoM2!SQ#rWhiD3GWX)SE-f6nftU7e%USH8oe_vnb9Ji|j7n{sQeU z^2u7LcTun6w783y5KW;E5d8r4lI2SeP7_IL`5(~qDnY(RnyxUBtAu!sG`&um-XKj^ zNz<*_BbJ)_EibN+<@iP<|&xtc0g{r5t*c_6x36V-fZ`}LyY+R(`y!c%z zb`AB-N=hwDP8BItO787uWkGWXI9q^sI7M%g%%7nNKj)voU+@p`JRZU=68bKu)h|ip zZBCU+CDMN6%2Mf@HUh^Y`No)bKr7F^Td zDfgdcWlB=irYq)!Eu>>3$Y;0Kt)+i3nbRW&)x literal 0 HcmV?d00001 diff --git a/spring24/week9/Sudoku.java b/spring24/week9/Sudoku.java new file mode 100644 index 0000000..2b4ea47 --- /dev/null +++ b/spring24/week9/Sudoku.java @@ -0,0 +1,225 @@ +package spring24.week9; + +import java.util.ArrayList; +import java.util.Scanner; + +/** + * A Sudoku puzzle is a 9x9 grid of numbers, where each row, column, and 3x3 subgrid contains the numbers 1-9 exactly once. + * + * The goal of this challenge is to write a program that can solve Sudoku puzzles. + * + * Given an input .sdku file (a plain-text file containing the initial state of a Sudoku puzzle), find the solution to the puzzle and write the solution to a new .sdku file. + * + * The input file will contain 9 lines, each containing 9 characters. Each character will be a digit (1-9) or a period (.), representing an empty cell. + * + * The output file should contain the same 9x9 grid, with the empty cells filled in with the correct numbers. + * + * For example, given the following input file: + * + * 53..7.... + * 6..195... + * .98....6. + * 8...6...3 + * 4..8.3..1 + * 7...2...6 + * .6....28. + * ...419..5 + * ....8..79 + * + * The output file should be: + * + * 534678912 + * 672195348 + * 198342567 + * 859761423 + * 426853791 + * 713924856 + * 961537284 + * 287419635 + * 345286179 + * + * This challenge is broken up into parts. In Programming Club, we will split into teams of 3-4 people to work on each method. + */ +public class Sudoku { + + public static void main(String[] args) { + System.out.println("Welcome to the Sudoku Solver!"); + System.out.println("Let's solve a Sudoku puzzle."); + + Scanner in = new Scanner(System.in); + System.out.print("Enter the name of the input file (do not include the file extension): "); + String inputFilename = in.nextLine(); + System.out.print("Enter the name of the output file (do not include the file extension): "); + String outputFilename = in.nextLine(); + + Sudoku app = new Sudoku(); + Cell[][] grid = app.readPuzzle(inputFilename); + app.solve(grid); + app.saveSolution(outputFilename, grid); + } + + /** + * Given an input filename, read the Sudoku puzzle from the file and return it as a 9x9 grid numbers, with no value for spaces in the file. + * + * @param filename + * @return + */ + public Cell[][] readPuzzle(String filename) { + System.out.println("TODO: Read sudoku puzzle solution from " + filename); + // TODO: Account for possible invalid input file and handle an error. + // TODO: Spaces in the file should be represented as '.' in the grid. + // TOOD: Read the file's puzzle. + return null; + } + + /** + * Given an output filename and a 9x9 grid of numbers, write the Sudoku puzzle to the file. + * + * @param grid + * @return + */ + public void saveSolution(String filename, Cell[][] grid) { + System.out.println("TODO: Write sudoku puzzle solution to " + filename); + // TODO: Account for possible invalid solution from isSolved and handle an error. + // TODO: Write the solution to the file. + } + + /** + * Determine what numbers are available to be placed in the given cell of the grid. + * + * @param grid + * @param row + * @param col + * @return an array of numbers that are available to be placed in the given cell + */ + private void getAvailableNumbers(Cell[][] grid, int row, int col) { + // TODO: Account for possible invalid inputs and handle an error. + // TODO: Given the rules of Sudoku, determine what numbers are available to be placed in the given cell of the grid. + // HINT: Use the getRowRemainingNumbers, getColRemainingNumbers, and getBoxRemainingNumbers methods and save to the Cell. + System.out.println("TODO: Get available numbers for cell at row " + row + " and column " + col); + } + + /** + * Solve the Sudoku puzzle. + * + * @param grid + */ + public void solve(Cell[][] grid) { + // TODO: Account for possible invalid inputs and handle an error. + // TODO: Given the rules of Sudoku, solve the puzzle. + + // HINT: Use the getAvailableNumbers method to find the available numbers for each cell. + // HINT: Use the isSolved method to check if the puzzle is solved. + // HINT: Assume the puzzle is solvable and has only one solution. + System.out.println("TODO: Solve the Sudoku puzzle"); + } + + /** + * Get any number between 1 and 9 that is not in the row. + * + * @param grid + * @param row + * @return + */ + private ArrayList getRowRemainingNumbers(Cell[][] grid, int row) { + ArrayList remainingNumbers = new ArrayList(); + for(int i = 1; i <= 9; i++) { + boolean found = false; + for(int j = 0; j < 9; j++) { + if(grid[row][j].getValue() == i) { + found = true; + break; + } + } + if(!found) { + remainingNumbers.add(i); + } + } + + return remainingNumbers; + } + + /** + * Get any number between 1 and 9 that is not in the column. + * + * @param grid + * @param col + * @return + */ + private ArrayList getColRemainingNumbers(Cell[][] grid, int col) { + ArrayList remainingNumbers = new ArrayList(); + for(int i = 1; i <= 9; i++) { + boolean found = false; + for(int j = 0; j < 9; j++) { + if(grid[j][col].getValue() == i) { + found = true; + break; + } + } + if(!found) { + remainingNumbers.add(i); + } + } + + return remainingNumbers; + } + + /** + * Get any number between 1 and 9 that is not in the box. + * + * A box is a 3x3 subgrid of the 9x9 grid. + * + * @param grid + * @param box + * @return + */ + private ArrayList getBoxRemainingNumbers(Cell[][] grid, int row, int col) { + ArrayList remainingNumbers = new ArrayList(); + int boxRow = row % 3; + int boxCol = col % 3; + for(int i = 1; i <= 9; i++) { + boolean found = false; + for(int j = 0; j < 3; j++) { + for(int k = 0; k < 3; k++) { + if(grid[boxRow * 3 + j][boxCol * 3 + k].getValue() == i) { + found = true; + break; + } + } + } + if(!found) { + remainingNumbers.add(i); + } + } + + return remainingNumbers; + } + + /** + * Given a list of numbers, return an array of the numbers. + * + * A helper method to keep the code using arrays instead of lists whenever possible. + * + * @param list + * @return + */ + private int[] arrayListToArray(ArrayList list) { + int[] array = new int[list.size()]; + for(int i = 0; i < list.size(); i++) { + array[i] = list.get(i); + } + return array; + } + + /** + * Given a 9x9 grid of numbers, return true if the grid is a valid Sudoku puzzle solution, and false otherwise. + * + * A valid Sudoku puzzle is one where each row, column, and 3x3 subgrid contains the numbers 1-9 exactly once. + * + * @param grid a 9x9 grid of numbers + * @return true if the grid is a valid Sudoku puzzle, and false otherwise + */ + private boolean isSolved(Cell[][] grid) { + return false; // TODO: I need to do this before club. + } +} \ No newline at end of file