@@ -239,68 +239,9 @@ class ProjectActivity : AppCompatActivity() {
239239 viewHolder : RecyclerView .ViewHolder ,
240240 target : RecyclerView .ViewHolder
241241 ): Boolean {
242- // if (viewHolder.adapterPosition == 0 ||
243- // target.adapterPosition == 0 ||
244- // listBlocks[target.adapterPosition].getBlockType() == "Begin" ||
245- // listBlocks[viewHolder.adapterPosition].getBlockType() == "Begin"){
246- // return true
247- // }
248- // else if (
249- // listBlocks[viewHolder.adapterPosition].getBlockType() == "ConditionIf" ||
250- // listBlocks[viewHolder.adapterPosition].getBlockType() == "ConditionIfElse" ||
251- // listBlocks[viewHolder.adapterPosition].getBlockType() == "Else" ||
252- // listBlocks[viewHolder.adapterPosition].getBlockType() == "CycleWhile"){
253- // val fromPosition = viewHolder.adapterPosition
254- // val toPosition = target.adapterPosition
255- // if (fromPosition > toPosition){
256- // val fromPosition2 = fromPosition + 1
257- // val toPosition2 = toPosition + 1
258- // Collections.swap(listBlocks, fromPosition, toPosition)
259- // Collections.swap(listBlocks, fromPosition2, toPosition2)
260- // blocksAdapter.notifyItemMoved(fromPosition, toPosition)
261- // blocksAdapter.notifyItemMoved(fromPosition2, toPosition2)
262- // }
263- // else if(fromPosition < toPosition &&
264- // (listBlocks[target.adapterPosition].getBlockType() == "ConditionIf" ||
265- // listBlocks[target.adapterPosition].getBlockType() == "ConditionIfElse" ||
266- // listBlocks[target.adapterPosition].getBlockType() == "Else" ||
267- // listBlocks[target.adapterPosition].getBlockType() == "CycleWhile")
268- // ){
269- // Collections.swap(listBlocks, fromPosition, toPosition)
270- // blocksAdapter.notifyItemMoved(fromPosition, toPosition)
271- // }
272- // else {
273- // val fromPosition2 = fromPosition + 1
274- // Collections.swap(listBlocks, fromPosition, fromPosition2)
275- // Collections.swap(listBlocks, fromPosition, toPosition)
276- // blocksAdapter.notifyItemMoved(fromPosition, fromPosition2)
277- // blocksAdapter.notifyItemMoved(fromPosition, toPosition)
278- // }
279- // return false
280- // }
281- // else if(
282- // listBlocks[target.adapterPosition].getBlockType() == "ConditionIf" ||
283- // listBlocks[target.adapterPosition].getBlockType() == "ConditionIfElse" ||
284- // listBlocks[target.adapterPosition].getBlockType() == "Else" ||
285- // listBlocks[target.adapterPosition].getBlockType() == "CycleWhile"){
286- // val fromPosition = viewHolder.adapterPosition
287- // val toPosition = target.adapterPosition
288- // if (fromPosition < toPosition){
289- // val toPosition2 = toPosition + 1
290- // Collections.swap(listBlocks, fromPosition, toPosition2)
291- // Collections.swap(listBlocks, fromPosition, toPosition)
292- // blocksAdapter.notifyItemMoved(fromPosition, toPosition2)
293- // blocksAdapter.notifyItemMoved(fromPosition, toPosition)
294- // }
295- // else {
296- // val toPosition2 = toPosition + 1
297- // Collections.swap(listBlocks, fromPosition, toPosition)
298- // Collections.swap(listBlocks, toPosition2, fromPosition)
299- // blocksAdapter.notifyItemMoved(fromPosition, toPosition)
300- // blocksAdapter.notifyItemMoved(toPosition2, fromPosition)
301- // }
302- // return false
303- // }
242+ if (viewHolder.adapterPosition == 0 || target.adapterPosition == 0 ){
243+ return true
244+ }
304245 val fromPosition = viewHolder.adapterPosition
305246 val toPosition = target.adapterPosition
306247 Collections .swap(listBlocks, fromPosition, toPosition)
@@ -310,30 +251,27 @@ class ProjectActivity : AppCompatActivity() {
310251
311252 override fun onSwiped (viewHolder : RecyclerView .ViewHolder , direction : Int ) {
312253 val position = viewHolder.adapterPosition
313- if (viewHolder.adapterPosition == 0 ||
314- listBlocks[position].type == " Begin" ||
315- listBlocks[position].type == " End" ||
316- listBlocks[position].type == " Else" ){
254+ val type = listBlocks[position].type
255+ if (viewHolder.adapterPosition == 0 || type == " Begin" || type == " End" || type == " Else" ){
317256 blocksAdapter.notifyItemRemoved(position)
318257 return
319258 }
320- else if ( listBlocks[position].type == " ConditionIf" ||
321- listBlocks[position].type == " ConditionIfElse" ||
322- listBlocks[position].type == " CycleWhile" ){
323- val type = listBlocks[position].type
324- listBlocks.remove(listBlocks[position])
259+ else if ( type == " ConditionIf" || type == " ConditionIfElse" || type == " CycleWhile" ){
325260 listBlocks.remove(listBlocks[position])
261+ var positionBegin = findNearBegin(position)
262+ listBlocks.remove(listBlocks[positionBegin])
326263 blocksAdapter.notifyItemRemoved(position)
327- blocksAdapter.notifyItemRemoved(position )
264+ blocksAdapter.notifyItemRemoved(positionBegin )
328265 var positionEnd = findNearEnd(position)
329266 listBlocks.remove(listBlocks[positionEnd])
330267 blocksAdapter.notifyItemRemoved(positionEnd)
331268 if (type == " ConditionIfElse" ){
332269 val positionElse = findNearElse(position)
333270 listBlocks.remove(listBlocks[positionElse])
334- listBlocks.remove(listBlocks[ positionElse] )
335- blocksAdapter.notifyItemRemoved(positionElse )
271+ positionBegin = findNearBegin( positionElse)
272+ listBlocks.remove(listBlocks[positionBegin] )
336273 blocksAdapter.notifyItemRemoved(positionElse)
274+ blocksAdapter.notifyItemRemoved(positionBegin)
337275 positionEnd = findNearEnd(positionElse)
338276 listBlocks.remove(listBlocks[positionEnd])
339277 blocksAdapter.notifyItemRemoved(positionEnd)
@@ -345,18 +283,34 @@ class ProjectActivity : AppCompatActivity() {
345283 blocksAdapter.notifyItemRemoved(position)
346284 }
347285 }
286+ private fun findNearBegin (start : Int ) : Int {
287+ for (i in start until listBlocks.size)
288+ if (listBlocks[i].type == " Begin" )
289+ return i
290+ for (i in 0 until start)
291+ if (listBlocks[i].type == " Begin" )
292+ return i
293+ return - 1
294+ }
348295 private fun findNearEnd (start : Int ) : Int {
349296 for (i in start until listBlocks.size)
350297 if (listBlocks[i].type == " End" )
351298 return i
299+ for (i in 0 until start)
300+ if (listBlocks[i].type == " End" )
301+ return i
352302 return - 1
353303 }
354304 private fun findNearElse (start : Int ) : Int {
355305 for (i in start until listBlocks.size)
356306 if (listBlocks[i].type == " Else" )
357307 return i
308+ for (i in 0 until start)
309+ if (listBlocks[i].type == " Else" )
310+ return i
358311 return - 1
359312 }
313+
360314 private fun createBlocksView (){
361315 val blocksView : RecyclerView = findViewById(R .id.blocksRV)
362316 blocksView.layoutManager = LinearLayoutManager (this )
@@ -383,7 +337,7 @@ class ProjectActivity : AppCompatActivity() {
383337 return ArrayList ()
384338 }
385339
386- private fun check (index : Int ) : Pair <Int , Int >{
340+ private fun connectionInBlock (index : Int ) : Pair <Int , Int >{
387341 var i = index
388342 while (listBlocks[i+ 1 ].type != " End" ){
389343 if (i+ 1 == listBlocks.size) {
@@ -394,9 +348,9 @@ class ProjectActivity : AppCompatActivity() {
394348 listBlocks[i].type == " ConditionIfElse" ||
395349 listBlocks[i].type == " CycleWhile" ){
396350 val temp = if (listBlocks[i].type == " ConditionIfElse" )
397- checkIfElse (i)
351+ connectionIfElse (i)
398352 else
399- checkIf (i)
353+ connectionIfOrWhile (i)
400354
401355 if (temp == 0 )
402356 return Pair (0 , 0 )
@@ -424,13 +378,13 @@ class ProjectActivity : AppCompatActivity() {
424378 return Pair (i, 0 )
425379 }
426380
427- private fun checkIfElse (index : Int ): Int {
381+ private fun connectionIfElse (index : Int ): Int {
428382 var i = index + 1
429383 if (listBlocks[i].type == " Begin" ){
430384 i + = 1
431385 if (listBlocks[i].type != " End" ){
432386 connectBlocks(listBlocks[index].begin, listBlocks[i])
433- val (j, temp) = check (i)
387+ val (j, temp) = connectionInBlock (i)
434388 if (j == 0 )
435389 return 0
436390 else if (listBlocks[temp].type == " End" ) {
@@ -452,7 +406,7 @@ class ProjectActivity : AppCompatActivity() {
452406 i + = 1
453407 if (listBlocks[i].type != " End" ){
454408 connectBlocks(listBlocks[index].beginElse, listBlocks[i])
455- val (i, temp) = check (i)
409+ val (i, temp) = connectionInBlock (i)
456410 if (i == 0 )
457411 return 0
458412 else if (listBlocks[temp].type == " End" ) {
@@ -482,7 +436,7 @@ class ProjectActivity : AppCompatActivity() {
482436 }
483437 }
484438
485- private fun checkIf (index : Int ): Int {
439+ private fun connectionIfOrWhile (index : Int ): Int {
486440 var i = index + 1
487441 if (listBlocks[i].type == " Begin" ){
488442 i + = 1
@@ -491,7 +445,7 @@ class ProjectActivity : AppCompatActivity() {
491445 return i + 1
492446 }
493447 connectBlocks(listBlocks[index].begin, listBlocks[i])
494- val (i, temp) = check (i)
448+ val (i, temp) = connectionInBlock (i)
495449 if (i == 0 )
496450 return 0
497451 else if (listBlocks[temp].type == " End" ) {
@@ -527,9 +481,9 @@ class ProjectActivity : AppCompatActivity() {
527481 listBlocks[i].type == " CycleWhile" ) {
528482
529483 val temp = if (listBlocks[i].type == " ConditionIfElse" )
530- checkIfElse (i)
484+ connectionIfElse (i)
531485 else
532- checkIf (i)
486+ connectionIfOrWhile (i)
533487
534488 if (temp == 0 ) return false
535489 else {
@@ -555,7 +509,7 @@ class ProjectActivity : AppCompatActivity() {
555509 blocksAdapter.saveAllData()
556510 for (i in 0 until listBlocks.size){
557511 listBlocks[i].indexListBlocks = i
558- listBlocks[i].crutch = true
512+ listBlocks[i].flagInit = true
559513 if (listBlocks[i].status != ok()){
560514 listBlocks[i].status = ok()
561515 blocksAdapter.notifyItemChanged(i)
@@ -567,11 +521,8 @@ class ProjectActivity : AppCompatActivity() {
567521 consoleAdapter.addMessage(programFinish(" Fail" ))
568522 return
569523 }
570- // printAllConnections()
571524 Block .isProgramRunning = true
572525 listBlocks[0 ].run ()
573- // printAllConnections()
574- // disconnectAllBlocks()
575526 }
576527
577528 private fun printAllConnections (){
0 commit comments